1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| if(clickX > posX) { if(clickY > posY) { posX --; posY ++; repaint(); } if(clickY < posY) { posX --; posY --; repaint(); } if(clickY == posY) { posX--; repaint(); } } |
not sure the issue of it not moving on the x-axis, but i do have these observations.
1. That should be if clickX < posX
2. use else if statements when you check your y position after already checking if the click is lower than the position. This prevents unnecessary checks from every having to execute.
3. why execute posX-- or posX++ inside every if statement. Since that is the one constant of the larger if statement, call it outside of your y-value checks, and only execute posY-- or posY++ inside your y-value checks.
4. there are certainly better ways to do this.
Gotta head to class now, but If no one else has provided a better solution, I'll reply again later tonight.