How do I target my random colour variable in my IF statement?

0

I have a question that is leading from the chosen best answer from my previous post: How can I use randomisation to specify different object parameters to a single iteration in a loop? I'm new to stack and wasn't sure of the best way to reference that post.

I have the code written as advised from the post above, however, now I'm attempting to have a method run different lines of code based off the colour of the 'brick' that is interacted with via my 'ball' object:

public Color brickColour;


public GameObj( int x, int y, int w, int h, Color c ){
        topX   = x;       
        topY = y;
        width  = w; 
        height = h; 
        colour = c;
}

public void initialiseGame(){
    Random random = new Random();
    int yellowBrick = random.nextInt(5);
    for (int i = 0; i < 5; i++) {
        brickColour = i == yellowBrick ? Color.YELLOWGREEN : Color.BLUE;
        GameObj brick = new GameObj(i*100, 100, BRICK_WIDTH, BRICK_HEIGHT, brickColour);
        brick.moveX(75);
        brick.visible = true;
        bricks.add(brick);
        System.out.println("Model:: Create Brick =" + brick);
    }
}

public synchronized void updateGame(){
           for(GameObj brick: bricks){
                if (ball.hitBy(brick)){
                    if(brickColour.equals(Color.YELLOWGREEN)){
                        ball.changeDirectionY();
                        addToScore(HIT_BRICK);
                        brick.visible = false;
                        Debug.trace("Model::Brick Hit YELLOWGREEN = " + brick);
                        startGame();
                    }else {
                        ball.changeDirectionY();
                        addToScore(HIT_BRICK);
                        brick.visible = false;
                        Debug.trace("Model::Brick Hit = " + brick);
                    }
                }
            }
}

I have tested the program and when the YELLOWGREEN 'brick' is hit, it's still running the else statement. I then printed the brickColour variable and got "0x0000ffff", though, even using that as a string in the color.equals() parameters, it didnt work. How exactly can I target the yellowgreen brick object?

java
javafx
asked on Stack Overflow Feb 18, 2020 by Timbo • edited Feb 19, 2020 by Timbo

1 Answer

3

Your problem lies in this:

brickColour = i == yellowBrick ? Color.YELLOWGREEN : Color.BLUE;

You have defined a global variable here and are using whatever the most recent set of this is. You should make this a local variable and not re-use it between methods.

for (int i = 0; i < 5; i++) {
    Color brickColour = i == yellowBrick ? Color.YELLOWGREEN : Color.BLUE;
    GameObj brick = new GameObj(i*100, 100, BRICK_WIDTH, BRICK_HEIGHT, brickColour);
    // omitted for answer
}

Then when doing your check:

for(GameObj brick: bricks){
    if (ball.hitBy(brick)){
        if(brick.brickColour == Color.YELLOWGREEN) {
             // yellow logic
        } else {
             // blue logic
        }
    }
}

Of course, using the colour itself is a rather poor design decision to begin with. Instead, you would want to have a property of the GameObj that declares its brick type, and have the colour determined by the brick type. But that's tangential to this discussion.

The lesson here is to make your variables scoped only to what is necessary. Don't use a global if you don't need to, use locals wherever you can.

answered on Stack Overflow Feb 18, 2020 by corsiKa

User contributions licensed under CC BY-SA 3.0