We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Block key input (Read 974 times)
Block key input
Sep 11th, 2006, 8:35pm
 
Hi,
I'm just wondering if there is a way to stop key input messages being actioned when a program is in a certain state. For example if an object moves in the Y-axis. If the Y-position of the object is greater than 50  key commands are recognised and actioned but if the Y-position is less than 50 key commands are ignored.

Thanks

Andy
Re: Block key input
Reply #1 - Sep 11th, 2006, 10:43pm
 
Just stack the conditions together.
Code:

float y = 0;
void setup(){
size(200,200);
smooth();
}
void draw(){
background(0);
ellipse(100, y, 20, 20);
y = (y + 1.5) % width;
}
void keyPressed(){
if(y > 100){
println("y > 100");
}
else{
println("y < 100");
}
}

Or:
Code:

float y = 0;
void setup(){
size(200,200);
smooth();
}
void draw(){
background(0);
ellipse(100, y, 20, 20);
y = (y + 1.5) % width;
if(keyPressed){
if(y > 100){
println("y > 100");
}
else{
println("y < 100");
}
}
}
Re: Block key input
Reply #2 - Nov 5th, 2006, 5:29pm
 
Hi again,
I am still struggling with this problem. Maybe I should explain a bit more about what I am trying to do.

In my spare time I am creating a simple 2D platform game. The character to be controlled can walk left and right, jump straight up and jump to the side.

My problem is that whilst my character is in the process of jumping any keys pressed stop the calculations that move the character up and down again, and the character just 'hangs' in mid-air.

The following code is what makes my character jump (I have only added code for the character facing/jumping right command to keep the content down).
Re: Block key input
Reply #3 - Nov 5th, 2006, 5:32pm
 
//*********************************************************
// Jump Button Functions
//*********************************************************

void Jump_Buttons_Pressed()
{
 if(keyCode == UP)
 {
   if (Is_Pete_Facing_Left == true)
   {
     Jump_Up_Face_Left();
   }
   
   else
   {
     Jump_Up_Face_Right();  
   }
 }
 
 //***********************************************
 
 if(key == ENTER)
 {
   if (Is_Pete_Facing_Left == true)
   {
     Jump_Left();
   }
   else
   {
     Jump_Right();
   }    
 }
 
}
Re: Block key input
Reply #4 - Nov 5th, 2006, 5:32pm
 
//*********************************************************
//Jump Straight Up Facing Right
//*********************************************************

void Jump_Up_Face_Right()
{

Pete_OffsetX = -10000;                      // Get rid of Pete Standing Picture whilst jumping pictures are on screen

 //*******************************************************

 if (PeteY > 219)                           // If Petes Y position is greater than 229 and no button is pressed
 {                                          // then velocity and gravity values equal zero
   velocityY = 0;
   gravity = 0;    
   Jump_Frame = 0;        
 }

 //*******************************************************

 if (PeteY > 219 &&                         // If petes Y position is greater than 229 and the up button is pressed
     keyCode == UP)                         // then velocity and gravity are set to environment constants
 {
   if (keyPressed == true)
   {
     velocityY = -20.0;
     gravity = 2.0;        
   }      
 }    

 //*******************************************************

 PeteY = PeteY + velocityY;  

 //*******************************************************

 if (PeteY < 220)                          // If pete is not on the floor show the jumping images
 {      
   velocityY = velocityY + gravity;        // If pete is off the floor allow the velocity value to change
   
   Jump_Frame = (Jump_Frame + 1) % JumpnumMoveRight;
   image(Pete_Jump_Face_Right[Jump_Frame], PeteX, PeteY);    
   
   if (Jump_Frame == 20)
    {
     Feet_Landing_Channel.play();
     Feet_Landing_Channel.volume(0.3);
    }  
 }  
}
Re: Block key input
Reply #5 - Nov 5th, 2006, 5:33pm
 
//*********************************************************
// Character Jump Right Commands
//*********************************************************

void Jump_Left()
{
   
  Pete_OffsetX = -10000;                      // Get rid of Pete Standing Picture whilst jumping pictures are on screen
 
 //*******************************************************

 if (PeteY > 219.0)                           // If Petes Y position is greater than 229 and no button is pressed
 {                                            // then velocity and gravity values equal zero
   velocityY = 0;
   gravity = 0;  
   Jump_Frame_Left = 0;            
 }

 //*******************************************************

 if (PeteY > 219.0 &&                         // If petes Y position is greater than 229 and the up button is pressed
 key == ENTER)                                // then velocity and gravity are set to environment constants
 {
   if (keyPressed == true)
   {
     velocityY = -20.0;
     gravity = 2.0;              
   }      
 }    

 //******************************************************* 

 PeteY = PeteY + velocityY;  

 //*******************************************************

 if (PeteY < 220)                             // If pete is not on the floor show the jumping images
 {      
   
    Jump_Frame_Left = (Jump_Frame_Left + 1) % JumpnumMoveRight_Left;
    image(Pete_Jump_Left[Jump_Frame_Left], PeteX, PeteY);
   
    if (Jump_Frame_Left == 20)
    {
     Feet_Landing_Channel.play();
     Feet_Landing_Channel.volume(0.3);
    }
 
 velocityY = velocityY + gravity;
   
    Pete_StartX = -10000;
    TopLeftX = TopLeftX + (3.5 *  Background_Movement);
    Man_HoleX = Man_HoleX + (3.5 *  Background_Movement);
    ShopsX = ShopsX + (3.5 *  Background_Movement);
    CratesX = CratesX + (3.5 *  Background_Movement);
   
    for (int i = 0; i < 15; i ++)
       {
         P1_L_X[i] = P1_L_X[i]  + (3.5 *  Background_Movement);
       }
       
    for (int i = 0; i < 15; i ++)
       {
         P1_R_X[i] = P1_R_X[i]  + (3.5 *  Background_Movement);
       }

     
    for (int i = 0; i < 35; i ++)
      {
        Silver_Vinyl_X_Pos[i] = Silver_Vinyl_X_Pos[i] + (3.5 * Background_Movement);
      }
     
    for (int i = 0; i < 15; i ++)
      {
        Gold_Vinyl_X_Pos[i] = Gold_Vinyl_X_Pos[i] + (3.5 * Background_Movement);
      }  
 }  
}
Re: Block key input
Reply #6 - Nov 5th, 2006, 5:34pm
 
I know this is a bit vague but does anybody have any ideas how I can block the key input and hence stop any keys pressed from affecting the jumping action of the character?

Many Thanks

Andy

(Sorry about the amount of content)
Re: Block key input
Reply #7 - Nov 5th, 2006, 9:45pm
 
What you have to do is to just keep track of if the player is in the air (set to true after jumping, false when player lands/dies whatever) and if the player is in the air, just ignore the key input.

So you'll probably want things like:

if(PeteY>219.0 && !inAir && key==ENTER)
{
 inAir=true;
 Do stuff..
}
Page Index Toggle Pages: 1