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
{
public int xspeed = 4;
public int yspeed = 4;
public static int BallX, BallY;
/** * Act - do whatever the paddle wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
setLocation(getX() + xspeed, getY()+yspeed);
bounce();
movement();
BallX = getX ();
BallY = getY ();
}
public void movement()
{
int worldWidth = getWorld().getWidth();
int worldHeight = getWorld().getHeight();
int spriteWidth = getImage().getWidth()/2;
int spriteHieght = getImage().getHeight()/2;
if(getX() <= spriteWidth)
xspeed = xspeed * -1;
if(getX() >= worldWidth - spriteWidth)
xspeed = xspeed * -1;
}
public void bounce ()
{
Actor mypaddle = getOneIntersectingObject (mypaddle.class) ;
if(mypaddle != null)
{
yspeed = yspeed * -1;
}
Actor paddle = getOneIntersectingObject (paddle.class) ;
if(paddle != null)
{
yspeed = yspeed * -1;
}
}
}
