TypeError: Error #1009: Cannot access a property or method of a null object reference.
Hi
Currently learning flash and as3 and just going over some of the basics at the moment. I just want to create a little cube that will move right and left and by reaching the right or left side of the scene he will move into the next scene. (Colliding into a symbol to signify either side) So he starts off in scene 1 moves to right and is shown in scene 2 if he then moved left again he would be back in scene 1.
Problem is I keep getting the error TypeError: Error #1009: Cannot access a property or method of a null object reference. Whenever it collides with the wall_mc
My code is
stop();
var leftArrow:Boolean = false;
var rightArrow:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_UP, nokeyHit);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
stage.addEventListener(Event.ENTER_FRAME, moveObject);
function keyHit(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.RIGHT :
rightArrow = true;
break;
case Keyboard.LEFT :
leftArrow = true;
break;
}
}
function nokeyHit(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.RIGHT :
rightArrow = false;
break;
case Keyboard.LEFT :
leftArrow = false;
break;
}
}
function moveObject(event:Event):void
{
if (leftArrow)
{
cube_mc.x -= 5;
}
if (rightArrow)
{
cube_mc.x += 5;
}
limitStageBorder(cube_mc);
}
function limitStageBorder(object:MovieClip)
{
var objectHalfWidth:uint = object.width / 2;
var objectHalfHeight:uint = object.height / 2;
if (object.x + objectHalfWidth > stage.stageWidth)
{
object.x = stage.stageWidth - objectHalfWidth;
}
else if (object.x - objectHalfWidth <0)
{
object.x = 0 + objectHalfWidth;
}
if (object.y - objectHalfHeight < 0)
{
object.y = 0 + objectHalfHeight;
}
else if (object.y + objectHalfHeight > stage.stageHeight)
{
object.y = stage.stageHeight - objectHalfHeight;
}
}
cube_mc.addEventListener(Event.ENTER_FRAME, cubeHit);
function cubeHit(event:Event):void
{
if (cube_mc.hitTestObject(wall_mc))
{
gotoAndPlay(1,"Scene 2");
}
else
{
null
}
}
If anyone knows where I am going wrong or what I need to change to make it work that would be great =] Thanks