Skip to main content
seongs97026980
Participating Frequently
May 24, 2017
Answered

Collision detection Error

  • May 24, 2017
  • 4 replies
  • 987 views

I'm working on as3 to make MC collision detection. Instances are king_stage and ball on stage.
I wrote on frame1 :

stop();
import flash.events.Event;

king_stage.addEventListener(Event.ENTER_FRAME, targetHit);
function targetHit(event:Event):void {
if (king_stage.hitTestObject(ball)) {
gotoAndStop(1, "Scene 2");
}
}

It works but received error message below, I don't know why. Please help.


TypeError: Error #1009: Cannot access a property or method of a null object reference.
at stop_jump_game_bomb2_fla::MainTimeline/targetHit()

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

The 1009 error indicates your code is trying to target something that does not exist.  Make sure you turn off that event listener before you move away from the objects that the code is trying to target - otherwise the ENTER_FRAME event continues to execute and if those items are gone, the error will erupt.

4 replies

seongs97026980
Participating Frequently
May 25, 2017

Thank you for the quick answer. I am really appreciate it.

Now, I know what is wrong with my script.

Would you tell me how to

" turn off that event listener before you move away from the objects before I move away from the objects " ?

scene 2 : on Frame 1:

button_3.addEventListener(MouseEvent.CLICK, fl_ClickToGoToPreviousScene);

function fl_ClickToGoToPreviousScene(event:MouseEvent):void
{
MovieClip(this.root).prevScene();
}

kglad
Community Expert
Community Expert
May 24, 2017

in addition to the above, you'll probably be able to conclude that king_stage and/or ball fail to exist in 'Scene 2'.

Colin Holgate
Inspiring
May 24, 2017

Also, go into Publish Settings, and under the SWF settings select the Permit Debugging option. That way you will be told which line number has the problem.

Ned Murphy
Ned MurphyCorrect answer
Legend
May 24, 2017

The 1009 error indicates your code is trying to target something that does not exist.  Make sure you turn off that event listener before you move away from the objects that the code is trying to target - otherwise the ENTER_FRAME event continues to execute and if those items are gone, the error will erupt.

seongs97026980
Participating Frequently
May 25, 2017

Thank you for the quick answer. I am really appreciate it.

Now, I know what is wrong with my script.

Would you tell me how to

" turn off that event listener before you move away from the objects before I move away from the objects " ?

scene 2 : on Frame 1:

button_3.addEventListener(MouseEvent.CLICK, fl_ClickToGoToPreviousScene);

function fl_ClickToGoToPreviousScene(event:MouseEvent):void
{
MovieClip(this.root).prevScene();
}

Colin Holgate
Inspiring
May 25, 2017

button_3.addEventListener(MouseEvent.CLICK, fl_ClickToGoToPreviousScene);

function fl_ClickToGoToPreviousScene(event: MouseEvent): void {

  button_3.removeEventListener(MouseEvent.CLICK, fl_ClickToGoToPreviousScene);

  MovieClip(this.root).prevScene();

}