Skip to main content
Arioman
Inspiring
March 12, 2013
Answered

using addEventListener for Enter and Exit frame

  • March 12, 2013
  • 1 reply
  • 11787 views
hi , sometimes i want to set some function in specific frame in start and exit this frame
for example i have Flv player in frame 20 , i have background sound ( mp3 player ) so i want when swf goes to this frame my sound goes Off and when Exit from it and go to another frames the sound played again ...
i put these script in frame 20 :
addEventListener(Event.ENTER_FRAME,enterfunc);
function enterfunc(e:Event):void {
    trace("I started");
}
addEventListener(Event.EXIT_FRAME,exitfunc);
function exitfunc(e:Event):void {
    trace("will Exit");
}
but after test movie i see I started and will Exit runs Repeatly without stoping !!
Notice : first i put my sndChannel = soundClip.play; and sndChannel = soundClip.stop;  but my music runs over and over and finally my system Hanged and need to Restart so i use trace to see

so whats a problem ?  thanks for spending time and help me

This topic has been closed for replies.
Correct answer kglad

because their names are misleading.  Event.EXIT_FRAME doesn't execute once when the playhead exits a frame.  it executes repeatedly.

likewise, Event.ENTER_FRAME doesn't execute once when the playhead enters a frame.  it executes repeatedly.

you can execute a function when the playhead enters a a frame by adding a function call to the frame:

f();  // attached to a frame will call the function f when the playhead enters this frame.  and there are other ways to do this.

you can execute a function when a playhead exists a frame by invalidating the stage and using a render event when a goto is executed:

stage.invalidate();

this.addEventListener(Event.RENDER,exitingF);

this.gotoAndPlay('whatever');

function exitingF(e:Event):void{

// this code executes when this frame is exited and 'whatever' is rendered (=displayed);

}

1 reply

kglad
Community Expert
March 12, 2013

those probably aren't going to do what you want but, if you wanted each to execute once, remove the event listener.

but again, i doubt that will do what you want.

you probaby want to use an Event.ADDED_TO_STAGE listener and Event.REMOVED_FROM_STAGE listener appied to your flvplayback component to trigger your background music to stop and play:

flv_pb.addEventListener(Event.ADDED_TO_STAGE, addedF);

flv_pb.addEventListener(Event.REMOVED_FROM_STAGE, removedF);

function addedF(e:Event):void{

sndChannel.stop();

}

function removedF(e:Event):void{

sndChannel=soundClip.play();

}

Arioman
AriomanAuthor
Inspiring
March 12, 2013

thanks Kglad .

i will test your tips tomorrow and as i know you i know it will help me as always ...

but just for know can you tell me why addEventListener and Event.EXIT_FRAME or Event.ENTER_FRAME runs repeatly and what script i must use if i want to add function in start or Exit frame at Once ?

cause maybe i want to use this in frames that have not Flv player or any Movieclip with instance name ...

am i forced to put some movieclip in frame and then use Event.ADDED_TO_STAGE and Event.REMOVED_FROM_STAGE script to them always ?!

kglad
Community Expert
March 12, 2013

thanks Dear Kglad for spending Time for me

your script works well in Enter Frame

but how i must use this for Exit Frame ?  am i must use removeEventListener for this ?



depending on exactly what you want and what's going on in the swf there are better and worse ways to detect what you want.