SOLVED: movement problem.
in
Programming Questions
•
5 months ago
Hey I am having problems with my program of moving a square on a grid using the arrow keys. I have everything set up and also the movement working with the keys but the square keeps duplicating each time I would press a key to move it along the grid and I would just like the single square to move on its own.
Can anyone help me?
Here's my code so far :
int sqrsize = 38;
int posx = 0;
int posy = 0;
void setup ()
{
size (300, 300);
background (255);
for (int posy = 0; posy < height; posy += sqrsize) {
for (int posx = 0; posx < width; posx += sqrsize) {
rect (posx, posy, sqrsize, sqrsize);
}
}
}
void draw () {
fill (0, 0, 255);
rect (posx, posy, sqrsize, sqrsize);
}
void keyPressed () {
if (key == CODED)
{
if (keyCode == RIGHT)
{
posx = posx + sqrsize;
}
if (keyCode == LEFT)
{
posx = posx - sqrsize;
}
if (keyCode == UP)
{
posy = posy - sqrsize;
}
if (keyCode == DOWN)
{
posy = posy + sqrsize;
}
}
}
1