Hey all,
i'm following the book and the videos while trying to wrap my head around the breakout game.
i can't see what's happening with the "int dist"
there is no value placed inside it anyware.
it's used in the ball and in the paddle but i just don't see how this is working.
section of the code:
full code:
download is available here
joc#27
/**
* Move the ball a given distance sideways.
*/
public void move(int dist)
{
setLocation (getX() + dist, getY());
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* The game paddle. It is keyboard controlled (left, right, space). It also
* generates a new ball when it is created itself.
*
* @author mik
* @version 1.1
*/
public class Paddle extends Actor
{
private Ball myBall; // used before ball gets released
private Counter counter;
public Paddle(Counter count)
{
counter = count;
}
/**
* When the paddle gets created, create a ball as well.
*/
public void addedToWorld(World world)
{
newBall();
}
/**
* 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()
{
if (Greenfoot.isKeyDown ("left")) {
moveSideways(-9);
}
if (Greenfoot.isKeyDown ("right")) {
moveSideways(9);
}
if (haveBall() && Greenfoot.isKeyDown ("space")) {
releaseBall();
}
}
private void moveSideways(int dist)
{
setLocation (getX() + dist, getY());
if (myBall != null) {
myBall.move (dist);
}
}
public void newBall()
{
myBall = new Ball(counter);
getWorld().addObject (myBall, getX(), getY()-20);
}
public boolean haveBall()
{
return myBall != null;
}
public void releaseBall()
{
myBall.release();
myBall = null;
}
}
