Copy link to clipboard
Copied
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()
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
button_3.addEventListener(MouseEvent.CLICK, fl_ClickToGoToPreviousScene);
function fl_ClickToGoToPreviousScene(event: MouseEvent): void {
button_3.removeEventListener(MouseEvent.CLICK, fl_ClickToGoToPreviousScene);
MovieClip(this.root).prevScene();
}
Copy link to clipboard
Copied
Thanks, Mr. Holgate,
I still have same problem with it. Please take a look at the screen shots.
Thank you in advance.
Copy link to clipboard
Copied
you didn't remove the event listener as suggested to resolve the problem.
function fl_ClickToGoToPreviousScene(event: MouseEvent): void {
button_3.removeEventListener(MouseEvent.CLICK, fl_ClickToGoToPreviousScene);
king_stage.removeEventListener(Event.ENTER_FRAME, targetHit);
MovieClip(this.root).prevScene();
}
Copy link to clipboard
Copied
Thank you for your effort, but it seems no change. I feel frustrated.
Copy link to clipboard
Copied
We gave the remove line for the button in scene 2, and you added a remove line for the enterframe listener, but you added the scene 1 enterframe listener remove to the scene 2 script. Delete the king_stage remove listener line from scene 2, and put it into the two functions in scene 1, before the line where you gotoAndStop to scene 2.
Copy link to clipboard
Copied
Thank you very very much!!!! It works.
Copy link to clipboard
Copied
I appreciate for your previous help. I got additional question for you about the collision detection game.
I made two balls on scene 1. When the ball_02(upper one) hits the king_stage, game works fine.
But the ball_01(lower one) hits the king_stage, I got a error message.
The game works no problem but it makes me curious. Please help me master! Thanks.
Copy link to clipboard
Copied
I appreciate for your previous help. I got additional question for you about the collision detection game.
I made two balls on scene 1. When the ball_02(upper one) hits the king_stage, game works fine.
But the ball_01(lower one) hits the king_stage, I got a error message.
The game works no problem but it makes me curious. Please help me master! Thanks
Copy link to clipboard
Copied
After you have removed the listener, and gotoAndStop(1, "Scene 2"), add:
return;
Otherwise when the ball_01 hits the king you will go to a frame where ball_02 doesn't exist, but you still do the hittest check on it. Adding the return; will make it exit the function instead.
Copy link to clipboard
Copied
Thanks a milion !
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
in addition to the above, you'll probably be able to conclude that king_stage and/or ball fail to exist in 'Scene 2'.
Copy link to clipboard
Copied
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();
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now