Skip to main content
ryanz67
Participant
May 5, 2015
Answered

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

  • May 5, 2015
  • 1 reply
  • 827 views

Hello, I have a problem with my ActionScript code. I have an object called carMove which is moved to the right using the arrow key. When this object hits an object called hitObject, I want it to display "Continue". This all works correctly but i'm getting a constant loop message.

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

  at HistoryOfSport_fla::MainTimeline/loop()

I think the problem occurs as soon as the carMove hits the object.

Any help please ?

Here is my code -

stage.focus = this;

import flash.events.KeyboardEvent;

import flash.events.Event;

var right:Boolean = false;

Continue.visible = false;

stage.addEventListener( KeyboardEvent.KEY_DOWN, keydown);

stage.addEventListener( KeyboardEvent.KEY_UP, keyup);

stage.addEventListener(Event.ENTER_FRAME, loop);

function keydown(e:KeyboardEvent):void

{

  switch ( e.keyCode )

{

  case 39 : right = true;{

  }

  break;

}

if (right) { carMove.x += 20 ; }

carMove.targetsecondObjectMc.rotation += 20;

carMove.targetcrownMc.rotation += 20;

}

if (x> 949.2) {

  gotoAndStop(3);

  }

function keyup(e:KeyboardEvent):void

{

  switch ( e.keyCode )

  {

  case 39 : right = false;

  break;

  }

}

function loop (e:Event) :void

{

  if ( carMove.hitTestObject( hitObject ) )

  {

  carMove.visible = false;

  Continue.visible = true;

  trace(carMove);

  }

}

//Adding an event Listener Continue button.

Continue.addEventListener( MouseEvent.MOUSE_UP, ContinueNext );

//Function for when button is pressed.

function ContinueNext( e:MouseEvent ):void

{

  //Go to frame 3.

  gotoAndStop(3);

}

This topic has been closed for replies.
Correct answer robdillon

That error is telling you that one or more of the objects referenced in the function loop isn't available where that function is written. So, if carMove, continue, or hitObject aren't on that frame then that's the origin of the error.

1 reply

robdillon
robdillonCorrect answer
Participating Frequently
May 5, 2015

That error is telling you that one or more of the objects referenced in the function loop isn't available where that function is written. So, if carMove, continue, or hitObject aren't on that frame then that's the origin of the error.

ryanz67
ryanz67Author
Participant
May 5, 2015

Thank you, managed to find the solution, just need to remove event listeners so they wouldn't carry on to the next frame.

event.currentTarget.removeEventListener(event.type,loop);

event.currentTarget.removeEventListener(KeyboardEvent.KEY_DOWN, keydown);