How do i make object to move from left to right? (that it will repeat it)
Also to go from up to down.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public void moveRight() { int x = (getX() + 5 ); int y = (getY()); setLocation(x, y); } public void moveLeft() { int x = (getX() - 5 ); int y = (getY()); setLocation(x, y); } public void moveDown() { int x = (getX()); int y = (getY() + 5 ); setLocation(x, y); } public void moveUp() { int x = (getX()); int y = (getY() - 5 ); setLocation(x, y); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public void moveOnKeyPress() { if (Greenfoot.isKeyDown( "left" )) { moveLeft(); } if (Greenfoot.isKeyDown( "right" )) { moveRight(); } if (Greenfoot.isKeyDown( "up" )) { moveUp(); } if (Greenfoot.isKeyDown( "down" )) { moveDown(); } } |