Hey,
I have a simple scenario where a ball simply bounces when it hits the wall. Turning 180 degrees does not bounce off according to the angle. In order for it to bounce off walls I used:
This works vertically but horizontally the ball gets stuck. Here is the code for the ball:
1 | setRotation(getRotation()*- 1 ); |
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 29 30 | import greenfoot.*; public class ball extends Actor { /** * Act - do whatever the ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move( 5 ); checkWalls(); } public void checkWalls(){ if (atWorldEdge()){ setRotation(getRotation()*- 1 ); } } public boolean atWorldEdge() { if (getX() < 10 || getX() > getWorld().getWidth() - 10 ) return true ; if (getY() < 10 || getY() > getWorld().getHeight() - 10 ) return true ; else return false ; } } |