We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'll make a snake game and when I press any other key in keyboard the circle stops, I wan't to deactivate all other keys and allow only arrows to move the snake.
int x, y;
void setup()
{
size(500,500);
}
void draw()
{
if (keyCode==UP)
{
y-=2;
}
if (keyCode==DOWN)
{
y+=2;
}
if (keyCode==LEFT)
{
x-=2;
}
if (keyCode==RIGHT)
{
x+=2;
}
ellipse(x,y,30,30);
}
Answers
@shideception you placed your key checking in the draw loop which means it will check the latest keycode, if you press your up key and then do nothing the latest keycode will de UP, but if you press 'c' it will stop moving your object since the latest pressed key was 'c'
The way I would solve this is by making a speed and then turn your draw into this
and instead of checking for pressed keys in the draw loop I would put them in the keyPressed void like this
so comments on up for explanation, dont forget to declare the speed integers
I don't see the point of checking the current speed values. Just always assign new ones:
@TfGuy44 you're right, It just to be
x += 2;
and then I checked to make sure it wont add more speed then 2 but since I changed it tox = 2;
it doesnt matter,