AS3 TypeError: Error #1009 Pong game
AS3 TypeError: Error #1009 Pong game
我知道有很多关于这个错误的帖子,但我是 AS3 的新手,我不知道如何使用这些具体答案中的任何一个来帮助我。
我在学校做一个项目,我不断得到
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at pong_fla::MainTimeline/loop()
我已经尝试了很多方法来尝试解决这个问题,但它仍然不断发生。
这是其引用的循环代码。
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;
}
}
我是 Whosebug 的新手,如果我可以改进我的问题请告诉我。
完整文件如下:
DropBox Link
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at pong_fla::MainTimeline/loop()
实际上这个错误说明了要做什么。 Flash 无法在您的舞台上找到 MC,或者 MC 没有那个 属性 可以使用 ;) 只需再次检查您的代码并使用跟踪来检测导致错误的原因。
当 ActionScript 在当前范围内找不到对象引用时,将引发此错误。我会仔细检查 playerPaddle 和 ball 是否都存在于舞台上,而不是在 MovieClips 中。请记住实例名称区分大小写,因此 Ball 不会与 ball 相同。
它发生是因为MovieClips
在其他框架中不存在
删除侦听器修复了大部分错误(但不是全部)
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;
}
我知道有很多关于这个错误的帖子,但我是 AS3 的新手,我不知道如何使用这些具体答案中的任何一个来帮助我。
我在学校做一个项目,我不断得到
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at pong_fla::MainTimeline/loop()
我已经尝试了很多方法来尝试解决这个问题,但它仍然不断发生。 这是其引用的循环代码。
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;
}
}
我是 Whosebug 的新手,如果我可以改进我的问题请告诉我。
完整文件如下: DropBox Link
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at pong_fla::MainTimeline/loop()
实际上这个错误说明了要做什么。 Flash 无法在您的舞台上找到 MC,或者 MC 没有那个 属性 可以使用 ;) 只需再次检查您的代码并使用跟踪来检测导致错误的原因。
当 ActionScript 在当前范围内找不到对象引用时,将引发此错误。我会仔细检查 playerPaddle 和 ball 是否都存在于舞台上,而不是在 MovieClips 中。请记住实例名称区分大小写,因此 Ball 不会与 ball 相同。
它发生是因为MovieClips
在其他框架中不存在
删除侦听器修复了大部分错误(但不是全部)
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;
}