Hello all,
I'm having trouble making my ball turn at the edge randomly all it wants to do is rotate the object.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Ball here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ball extends Actor
{
private int deltaY = 2;
private int deltaX = 2;
/**
* 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()
{
setLocation(getX() , getY() +deltaY);// Add your action code here.
if (atWorldEdge())
{
deltaY = deltaY * (-1);
setLocation(getX() +deltaX, getY() );
//turn(Greenfoot.getRandomNumber(90)+20); // rotate object not turn
//setRotation(Greenfoot.getRandomNumber(50));
}
}
/**
* Test if we are close to one of the edges of the world. Return true is we are.
*/
public boolean atWorldEdge()
{
if(getX() < 20 || getX() > getWorld().getWidth() - 20)
return true;
if(getY() < 20 || getY() > getWorld().getHeight() - 20)
return true;
else
return false;
}
}