Search code examples
actionscript-3flashloopstypeerrorpong

AS3 TypeError: Error #1009 Pong game


I know there are alot of posts about this error but im new at AS3 and i can't figure out how to use any of these specific answers to help me.

Im working on a project at school and I keep getting

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at pong_fla::MainTimeline/loop()

I have tried alot of things to try and fix this but it still keeps occuring. Here is the code with the loop its referring to.

var ballSpeedX: int = -6;
var ballSpeedY: int = -6;
var cpuPaddleSpeed: int = 3;
var playerScore: int = 0;
var cpuScore: int = 0;
var wintotal: int = 1;

init();
function init(): void {
    stage.addEventListener(Event.ENTER_FRAME, loop);
}
function calculateBallAngle(paddleY: Number, ballY: Number): Number {
    var ySpeed: Number = 5 * ((ballY - paddleY) / 25);
    return ySpeed;
}
function updateTextFields(): void {
    playerScoreText.text = ("Player Score: " + playerScore);
    cpuScoreText.text = ("CPU Score: " + cpuScore);
}
function loop(e: Event): void {
    if (playerScore == wintotal) {
        gotoAndStop(3);
    }
    if (cpuScore == wintotal) {
        gotoAndStop(4);
    }
    if (playerPaddle.hitTestObject(ball) == true) {
        if (ballSpeedX < 0) {
            ballSpeedX *= -1;
            ballSpeedY = calculateBallAngle(playerPaddle.y, ball.y);
        }
    } else if (cpuPaddle.hitTestObject(ball) == true) {
        if (ballSpeedX > 0) {
            ballSpeedX *= -1;
            ballSpeedY = calculateBallAngle(cpuPaddle.y, ball.y);
        }
    }
    if (cpuPaddle.y < ball.y - 10) {
        cpuPaddle.y += cpuPaddleSpeed;
    } else if (cpuPaddle.y > ball.y + 10) {
        cpuPaddle.y -= cpuPaddleSpeed;
    }
    playerPaddle.y = mouseY;
    if (playerPaddle.y - playerPaddle.height / 2 < 0) {
        playerPaddle.y = playerPaddle.height / 2;
    } else if (playerPaddle.y + playerPaddle.height / 2 > stage.stageHeight) {
        playerPaddle.y = stage.stageHeight - playerPaddle.height / 2;
    }
    ball.x += ballSpeedX;
    ball.y += ballSpeedY;
    if (ball.x <= ball.width / 2) {
        ball.x = ball.width / 2;
        ballSpeedX *= -1;
        cpuScore++;
        updateTextFields();
    } else if (ball.x >= stage.stageWidth - ball.width / 2) {
        ball.x = stage.stageWidth - ball.width / 2;
        ballSpeedX *= -1;
        playerScore++;
        updateTextFields();
    }
    if (ball.y <= ball.height / 2) {
        ball.y = ball.height / 2;
        ballSpeedY *= -1;
    } else if (ball.y >= stage.stageHeight - ball.height / 2) {
        ball.y = stage.stageHeight - ball.height / 2;
        ballSpeedY *= -1;
    }
}

I am new to StackOverflow, if i can improve on my question plaese let me know.

Here is the full file: DropBox Link


Solution

  • it happens because MovieClips don't exist in other frames
    removing the listener fixed most of the errors (not all of them, however)

    if (playerScore == wintotal) {
        //remove the listener when leaving the frame
        stage.removeEventListener(Event.ENTER_FRAME, loop);
        gotoAndStop(3);
    }
    if (cpuScore == wintotal) {
        //remove the listener when leaving the frame
        stage.removeEventListener(Event.ENTER_FRAME, loop);
        gotoAndStop(4);
    }
    //check if MovieClips exist
    if(!playerPaddle || !cpuPaddle){
        return;
    }