Skip to main content
Participant
November 14, 2012
Answered

TypeError: Error #1009: Cannot access a property or method of a null object reference.

  • November 14, 2012
  • 1 reply
  • 1082 views

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

This topic has been closed for replies.
Correct answer Ned Murphy

Line 46 of the code in frame 1 ins where the problem lies.  Which line is it?

1 reply

Ned Murphy
Legend
November 14, 2012

The 1009 error indicates that one of the objects being targeted by your code is out of scope.  This could mean that the object....

 

- is declared but not instantiated

- doesn't have an instance name (or the instance name is mispelled)

- does not exist in the frame where that code is trying to talk to it

- is animated into place but is not assigned instance names in every keyframe for it

- is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s).

 

If you go into your Publish Settings Flash section and select the option to Permit debugging, your error message should have a line number following the frame number which will help you isolate which object is involved.

Participant
November 14, 2012

TypeError: Error #1009: Cannot access a property or method of a null object reference.

          at Untitled_fla::MainTimeline/moveObject()[Untitled_fla.MainTimeline::frame1:46]

Debug session terminated.

Would that be it?

Ned Murphy
Ned MurphyCorrect answer
Legend
November 14, 2012

Line 46 of the code in frame 1 ins where the problem lies.  Which line is it?