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:
setRotation(getRotation()*-1);
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;
}
}

