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.
IndexDiscussionExhibition › More help with motion
Page Index Toggle Pages: 1
More help with motion (Read 750 times)
More help with motion
May 23rd, 2009, 6:53pm
 
More about the snake game.  This is what I have so far.  I have background commented out in the display function so its clear whats happening.  After pressing 'w' the loop only iterates 6 times then stops.  I assume this means that keypressed changes to false after a short period of time.  Is there any way to get it stay true indefinitly?  Then the idea is that it will keep moving until another key is pressed to change its direction.  Then it will keep moving in that direction until changed and so on.  Thanks for any help.

float xvel = 0;
float yvel = 0;
float xpos = 300;
float ypos = 300;

void setup()
{
 size(600, 600);
 background(102);
 smooth();
 rectMode(CENTER);
 rect(xpos,ypos,20,20);
}
//*************************************************
void draw()
{
 if (keyPressed == true)
 {
   
     if (key == 'w')
     {
       yvel=-30;
       move();
     }
   
 }
 display();
}
   
//***************************************************    

void move()
{
   xpos += xvel;
   ypos += yvel;
}
//************************************************
void display()
{
 //background(102);
 rectMode(CENTER);
 rect(xpos,ypos,20,20);
}
Re: More help with motion
Reply #1 - May 23rd, 2009, 10:34pm
 
On my system, it continues as long as I hold the key down.

Maybe something to do with your keyboard settings?.

To avoid this, you could use a boolean variable to store the key input: Code:
void keyPressed() {
 if (key == 'w') {
   wFlag = true;
 }
}

then in draw() test for wFlag instead of keyPressed.
Re: More help with motion
Reply #2 - May 24th, 2009, 1:02am
 
And avoid multiplying threads on the same topic.
Page Index Toggle Pages: 1