How can i code the ball to bounce off the worlds edge when it hits either the top,bottom right or left edge?
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;
}
private void shoot()
{
if("f".equals(Greenfoot.getKey()))
{
Bullet bullet=new Bullet();
getWorld().addObject(bullet,getX(), getY());
bullet.setRotation(getRotation());
bullet.move(75);
}
} private void shoot()
{
if("p".equals(Greenfoot.getKey()))
{
Bullet2 bullet2=new Bullet2();
getWorld().addObject(bullet2,getX(), getY());
bullet2.setRotation(getRotation());
bullet2.move(75);
}String key = Greenfoot.getKey();
if ("f".equals(key)) // code to have Tank object shoot a Bullet
if ("p".equals(key)) // code to have Tank2 object shoot a Bullet2// instance field
private boolean shotKeyDown;
// in act method
if (!shotKeyDown && Greenfoot.isKeyDown("f"))
{
shoot();
shotKeyDown = true;
}
if (shotKeyDown && !Greenfoot.isKeyDown("f")) shotKeyDown = false;String key = Greenfoot.getKey();
if ("f".equals(key)) // code to have Tank object shoot a Bullet
if ("p".equals(key)) // code to have Tank2 object shoot a Bullet2// instance field
private boolean shotKeyDown;
// in act method
if (!shotKeyDown && Greenfoot.isKeyDown("f"))
{
shoot();
shotKeyDown = true;
}
if (shotKeyDown && !Greenfoot.isKeyDown("f")) shotKeyDown = false;