Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Collision detection Error

Community Beginner ,
May 23, 2017 May 23, 2017

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()

TOPICS
ActionScript
850
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 24, 2017 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.

Translate
LEGEND ,
May 24, 2017 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 25, 2017 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();
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 25, 2017 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();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 25, 2017 May 25, 2017

Thanks, Mr. Holgate,

I still have same problem with it. Please take a look at the screen shots.

Thank you in advance.

scene1.jpgscene2.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 25, 2017 May 25, 2017

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();

}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 25, 2017 May 25, 2017

Thank you for your effort, but it seems no change. I feel  frustrated.scene2_frame1.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 26, 2017 May 26, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 26, 2017 May 26, 2017

Thank you very very much!!!!   It works.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 29, 2017 May 29, 2017

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.scene1_frame1.jpgscene2_frame1.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 30, 2017 May 30, 2017

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! Thanksscene1.pngscene2.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 31, 2017 May 31, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 31, 2017 May 31, 2017
LATEST

Thanks a milion !

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 24, 2017 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2017 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'.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 25, 2017 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();
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines