TypeError: Error #1009: Cannot access a property or method of a null object reference
Hey all. I checked Google and saw tons of results. Read some of the answers and tried to apply the solutions they used. Either it doesn't work or I'm doing something wrong.
I kinda understand the error, but I don't know how to correct it.
This is what I have in one frame. I know, I know...I'll use classes next time. Promise. :crazy:
The error only appeared when I implemented the following bit of code:
Code:
if(mcBall.hitTestObject(man))
{
gotoAndPlay("gameOver");
}
Code:
stop();
// VARIABLES
var ballXSpeed:Number = -8;
var ballYSpeed:Number = -8;
var starXSpeed:Number = 8;
var starYSpeed:Number = 8;
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
var manSpeed:Number = 7;
// MOVE RED CIRCLE
mcBall.addEventListener(Event.ENTER_FRAME, moveBall);
function moveBall(event:Event):void
{
mcBall.x += ballXSpeed;
mcBall.y += ballYSpeed;
if(mcBall.x >= stage.stageWidth-mcBall.width)
{
ballXSpeed *= -1;
}
if(mcBall.x < 0)
{
ballXSpeed *= -1;
}
if(mcBall.y >= stage.stageHeight-mcBall.height)
{
ballYSpeed *= -1;
}
if(mcBall.y <0)
{
ballYSpeed *= -1;
}
}
// MOVE STAR
mcStar.addEventListener(Event.ENTER_FRAME, moveStar);
function moveStar(event:Event):void
{
mcStar.x += starXSpeed;
mcStar.y += starYSpeed;
if(mcStar.x >= stage.stageWidth-mcStar.width)
{
starXSpeed *= -1;
}
if(mcStar.x < 0)
{
starXSpeed *= -1;
}
if(mcStar.y >= stage.stageHeight-mcStar.height)
{
starYSpeed *= -1;
}
if(mcStar.y <0)
{
starYSpeed *= -1;
}
}
man.addEventListener(Event.ENTER_FRAME, moveMan)
// MOVE PLAYER
function moveMan(event:Event):void
{
Mouse.hide();
man.x = mouseX;
man.y = mouseY;
}
// COLLISION CIRLE AND MAN
// The culprit for messing up
stage.addEventListener(Event.ENTER_FRAME, circleHits);
function circleHits(event:Event):void
{
if(mcBall.hitTestObject(man))
{
gotoAndPlay("gameOver");
}
}
When the collision occurs, it goes to the game over screen but in flash it generates an infinite loop until I close the application.
Here's the bit that loops.
Code:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at game_fla::MainTimeline/moveBall()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at game_fla::MainTimeline/moveStar()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at game_fla::MainTimeline/moveMan()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at game_fla::MainTimeline/circleHits()
Thanks. :cap: