This site requires JavaScript, please enable it in your browser!
Greenfoot back
Quickske
Quickske wrote ...

2018/12/24

help with breakout

Quickske Quickske

2018/12/24

#
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:
/**
     * Move the ball a given distance sideways.
     */
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
    }
full code:
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;
    }
        
}

download is available here joc#27
danpost danpost

2018/12/24

#
Quickske wrote...
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.
Look at lines 35 and 38. A '-9' and a '9' are used to call moveSideways, which uses those values to call move(int) on the ball at line 50..
Quickske Quickske

2018/12/24

#
i'm sorry, but i still don't understand is. :s i'm @ work now and i know it's x-mass eve, but would it be possible to clarify it a bit further? thanks
danpost danpost

2018/12/24

#
Quickske wrote...
i'm sorry, but i still don't understand is. :s would it be possible to clarify it a bit further?
Please refer to this page of the java tutorials.
You need to login to post a reply.