How to move an object diagonally using keycode
in
Programming Questions
•
5 months ago
Right now, I am only able to move my rectangle up, down, left, and right. This is what I tried to get it to move diagonally but it doesn't work
- rect(x, y, 15, 15);
- if (key== CODED) {
- //strokeWeight(5);
- if (keyCode == DOWN) {
- y+=2;
- }
- else if (keyCode == RIGHT) {
- x+=2;
- }
- else if (keyCode == UP) {
- y-= 2;
- }
- else if (keyCode == LEFT) {
- x-=2;
- }
- else if (keyCode == UP && keyCode == RIGHT) {
- x+=2;
- y-=2;
- }
- else if (keyCode == UP && keyCode == LEFT) {
- x-=2;
- y-=2;
- }
- else if (keyCode == DOWN && keyCode == LEFT) {
- x+=2;
- y+=2;
- }
- else if (keyCode == DOWN && keyCode == RIGHT) {
- x-=2;
- y+=2;
- }
1