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

2016/4/6

Public boolean help?

OrientalCarpet OrientalCarpet

2016/4/6

#
Sorry if this is a dumb question, but I'm having trouble with a public boolean.
1
2
3
4
5
6
7
8
public class Map extends World
{
    Brick brick = new Brick();
    public final int GAME_OVER = 3;
    public int state = MENU;
    private int level = 0;
    private boolean spacePressed = false;
    public boolean ballIn = false;
I'm using the public boolean ballIn in my World class. then, in an actor class, I call upon the boolean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Ball extends Actor
{
    int dx = Greenfoot.getRandomNumber(6) - 3;
    int dy = 7;
    /**
     * 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()
    {
        if(ballIn == true){
if (getX() <= width/2 || getX() >= getWorld().getWidth() - width/2) {
                dx = -1*dx;
            }
for some reason, when i compile the code, an error regarding the ballIn == true is brought up, saying that "cannot find symbol - variable ballIn"
danpost danpost

2016/4/6

#
The 'ballIn' field is declared in the Map class, not in the Ball class. It is possible to access the value of the field in the Map class from the Ball class, but you have to get it through the Map world object the ball is in. To properly assist you, I would request a brief description as to what the 'ballIn' boolean field is precisely used for.
OrientalCarpet OrientalCarpet

2016/4/7

#
so im creating the classic game where a paddle bounces a ball up to the layout of bricks and destroys them. this section of code is working on finding when the ball is no longer on the screen: if the ball touches the bottom of the screen, it is taken out and the game is set to "Game Over" mode. however, when I am on the title screen, the ball is also not present, so I'm using this ballIn variable to keep track of when the ball is present in the game.
danpost danpost

2016/4/7

#
OrientalCarpet wrote...
when I am on the title screen, the ball is also not present
Are you using an Actor subclass or a World subclass for the title screen?
OrientalCarpet OrientalCarpet

2016/4/7

#
I am using an actor subclass for it
danpost danpost

2016/4/7

#
OrientalCarpet wrote...
I am using an actor subclass for it
You could ask if the world has the title screen showing or not with something like the following in the World subclass act method (I will call your actor subclasses TitleScreen and GameOver):
1
2
3
4
5
6
if (getObjects(TitleScreen.class).isEmpty() &&
    getObjects(GameOver.class).isEmpty() &&
    getObjects(Ball.class).isEmpty())
{
    spawnBall();
}
The will cause a ball to spawn only while the game is in progress. If the title screen actor or the game over actor is in the world, or a ball is already in the world, no ball will spawn.
You need to login to post a reply.