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

2011/10/5

Starting an action

MLuedke MLuedke

2011/10/5

#
Hi, I'm kind of new to this so the solution is probably pretty simple. I wanted to remake a simple game like Breakout, platform on the bottom, bricks along the top, ball bounces and breaks the bricks and you keep it from hitting the bottom with the platform. I got all the basics, but now I want the ball to follow just above the platform at the very beginning and then when you hit a button it begins hitting bricks. I got the ball to follow but I don't know how to get the ball to start moving on its own and out of your control.
danpost danpost

2011/10/6

#
Sounds like you need an instance variable of type boolean added to the ball class, called something like isFollowing. Start it off as true. When a key is pressed, switch it to false. In the balls act method, check the value to see what action needs to be done (follow platform or bounce).
MLuedke MLuedke

2011/10/9

#
I haven't even touched boolean, how do you set it up so that it switches from true to false at the press of a button. Also how do you set what true and false do.
mjrb4 mjrb4

2011/10/9

#
There's no setting what true and false "do" directly - you can check the value of a boolean variable such as isFollowing and then do something if it's true or false:
if(isFollowing) {
    //do if true
}
else {
    //do if false
}
In terms of switching from true to false and vice versa, the following does the trick:
isFollowing = !isFollowing;
The ! in this example means NOT, i.e. invert the value of isFollowing.
danpost danpost

2011/10/9

#
The above 'switch' can be placed within the conditional expression as follows:
if (isFollowing && Greenfoot.getKey() != null) {
        isFollowing = !isFollowing; // or      isFollowing = false;
    } // the previous updates the boolean variable 'isFollowing' (if we're following and a key is pressed, stop following)
    // now we can act on the value of the boolean variable
    if (isFollowing) {
        // code to continue following platform
    }
    else {
        // code to move freely
    }
MLuedke MLuedke

2011/10/11

#
That does help, however the code I have for following the platform won't work in that format so it brings up another question. I want to set it up so it looks somewhat like this
    {
        setLocation(getX() + 0, getY() + 5);        
    }
It is simple, but I'm not sure how to make that be the offset of the ball from the platform. I just don't know where to place the Platform.class or how to add that into the code.
danpost danpost

2011/10/11

#
You could give the Ball a reference to the Platform via instance variable (Platform platform). You need to make sure the platform is created first. Then, in the act() method of Ball.class, in the 'if (isFollowing)' code put 'setLocation(platform(getX(), platform.getY() + 5);'. That should work for following the platform.
MLuedke MLuedke

2011/10/13

#
Well, I wrote up everything and I'm pretty sure it'll work but the reference isn't working for some reason. I keep getting the error, "non-static method getY() cannot be referenced from a static context." I was just guessing at the reference for the actor, I know how to reference methods but not an actor or object.
You need to login to post a reply.