PDA

View Full Version : do something after hittest " remove hittest after first collsion"



flashman2006
February 8th, 2010, 06:57 PM
hi there i am stuck and need help please

i am controlling a tractor mc with right and left keyboard keys and let the user move the tractor till it reach a certain point where it touch a mc

i want to go to a certain frame and stop the keyboard event listener and also the hit test listener

so finally the tractor is moved by user till it touches the mc then we will jump to a certain frame

this certain frame will not have both of the tractor and the mc

i used this code


import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.display.MovieClip;
import flash.events.*;

stop();

tractor.removeEventListener(Event.EXIT_FRAME, circleHit);
stage.removeEventListener(Event.EXIT_FRAME, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

function keyDownHandler(event:KeyboardEvent):void {



if(event.keyCode == Keyboard.LEFT){
tractor.w1.rotation -= 1 ;
tractor.w2.rotation -= 1.5 ;
var cartween : Tween = new Tween (tractor, "x", Regular.easeOut, tractor.x, tractor.x -1, 1, false);
}
if(event.keyCode == Keyboard.RIGHT){
tractor.w1.rotation += 1 ;
tractor.w2.rotation += 1.5 ;;
var cartween2 : Tween = new Tween (tractor, "x", Regular.easeOut, tractor.x, tractor.x +1, 1, false);
}
}

tractor.addEventListener(Event.ENTER_FRAME, circleHit);
function circleHit(event:Event) {
if(tractor.hitTestObject(line)){
trace ("hit");
gotoAndPlay ("tractor_play");

}
}



the flash jumps to the frame "tractor_play"

but

i got this error


TypeError: Error #1009: Cannot access a property or method of a null object reference.
at site2_fla::main_2/circleHit()

IQAndreas
February 8th, 2010, 07:12 PM
Here you go: :)


tractor.addEventListener(Event.ENTER_FRAME, circleHit);

function circleHit(event:Event)
{
if(tractor.hitTestObject(line))
{
tractor.removeEventListener(Event.ENTER_FRAME, circleHit);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

trace ("hit");
gotoAndPlay ("tractor_play");
}
}

BoppreH
February 8th, 2010, 07:15 PM
I think you already understood the error and why you should remove the listeners, but I'm breaking down the problems anyway. Jump to the end for the actual code.

1) Move the function "circleHit" to outside the keyDownHandler. Functions should not be nested, specially in a needless case like this.

2) The error comes from the fact that the listener is still alive after you make the "frame jump" and the tractor does not exist anymore. So the method call "tractor.hitTestObject" can not be made and an error is thrown.

3) To solve the error, you must remove the event listener before going to the frame where there's no tractor. Same thing goes for the stage's keyboard listener.


import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.display.MovieClip;
import flash.events.*;

stop();

tractor.removeEventListener(Event.EXIT_FRAME, circleHit);
stage.removeEventListener(Event.EXIT_FRAME, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

function keyDownHandler(event:KeyboardEvent):void {



if(event.keyCode == Keyboard.LEFT){
tractor.w1.rotation -= 1 ;
tractor.w2.rotation -= 1.5 ;
var cartween : Tween = new Tween (tractor, "x", Regular.easeOut, tractor.x, tractor.x -1, 1, false);
}
if(event.keyCode == Keyboard.RIGHT){
tractor.w1.rotation += 1 ;
tractor.w2.rotation += 1.5 ;;
var cartween2 : Tween = new Tween (tractor, "x", Regular.easeOut, tractor.x, tractor.x +1, 1, false);
}
}

tractor.addEventListener(Event.ENTER_FRAME, circleHit);
}

function circleHit(event:Event) {
if(tractor.hitTestObject(line)){
tractor.removeEventListener(Event.ENTER_FRAME, circleHit)
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
trace ("hit");
gotoAndPlay ("tractor_play");
}

flashman2006
February 9th, 2010, 02:58 AM
Many thanks for your reply

my problem is solved but another one appears

my tractor movie clip has another mc inside it containing a looping sound after the movie jumps to "tractor_play"

the sound still playing while the main tractor mc doesn't exist ?

BoppreH
February 9th, 2010, 09:53 AM
Many thanks for your reply

my problem is solved but another one appears

my tractor movie clip has another mc inside it containing a looping sound after the movie jumps to "tractor_play"

the sound still playing while the main tractor mc doesn't exist ?

It's because the tractor was removed from the display list, but it still exists.

Objects are garbage collected (permanently erased) when there are no references to them (variables set to their instances or event listeners). Set to null all variables that are referencing it along all event listeners that were added.

flashman2006
February 9th, 2010, 10:06 AM
sry i don't know how to do this

the tractor instance is

tractor