Hi,
I'm trying to create a world where the object moves and when it hits the edge it bounces back into the opposite direction. I figured out how to make it move to the other side but not how to bounce it off.
Here's my code so far-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public void act() { move(+ 12 ); if (getX() == (getWorld().getWidth()- 1 )) { setLocation( 0 , getY()); } else if (getX() == 0 ) { setLocation(getWorld().getWidth(), getY()); } if (getY() == (getWorld().getHeight()- 1 )) { setLocation(getX(), 0 ); } else if (getY() == 0 ) { setLocation(getX(), getWorld().getHeight()); } } } |