zygisk123
zygisk123

Reputation: 9

breakout game in Android studio

Hey I'm trying to create a breakout game using Android studio, but I get an error...

Problem

I have successfully created a bricks map on the game screen however, when I perform the ball.collision check, the program crashes...

What I tried

I created an array of bricks and if brick.isDestroyed == false then I display the brick on the game screen. It succeeds

        for (y = 0; y < 5; y++){
            for (x = 0; x < 14; x++){
                addX = (brickWidth + 10) * x;
                addY = (brickHeight + 10) * y;
                bricks[id] = new Brick(sprite, canvas, (int) positionX + addX, (int) positionY + addY);
                id++;
            }
        }

        for (int i = 0; i < NumOfBricks; i++){
            if(!bricks[i].isDestroyed){
                bricks[i].draw();
            }
        }

I created a collision method in the ball class

        if (ballX > target.x + target.width || target.x > ballX + radius / 2){
            return false;
        }
        if (ballY > target.y + target.height || target.y > ballY + radius / 2){
            return false;
        }
        return true;
    }

I call this function in the update method (Game class)

        bricks = level.bricks;
        for (int i = 0; i < level.NumOfBricks; i++){
            Brick brick = bricks[i];
            if (ball.collides(brick)){
                brick.isDestroyed = true;
            }
        }

However when I run my code the program crashes... I am new to Java language. I would be very grateful if someone could help me. Please, feel free for checking my code https://github.com/zygisk123/AndroidStudio

ERROR (logCat):

2022-09-14 03:39:35.492 4016-4123/com.example.breakoutgame E/AndroidRuntime: FATAL EXCEPTION: Thread-2
    Process: com.example.breakoutgame, PID: 4016
    java.lang.NullPointerException: Attempt to read from null array
        at com.example.breakoutgame.Game.update(Game.java:124)
        at com.example.breakoutgame.GameLoop.run(GameLoop.java:55)

Upvotes: 0

Views: 40

Answers (0)

Related Questions