xdl
xdl

Reputation: 1110

Flash as3 event listener for when a movieclip is stopped

I'm thinking of making a movieclip which stops itself at certain points (just with stop(); at certain keyframes in the movieclip timeline), then the user is prompted to continue the clip.

What is the best why to listen for the stopping of the movieclip? I think I want an event listener that detects when the movieclip is stopped, but I don't know if there is one.

Many thanks

Upvotes: 2

Views: 7371

Answers (4)

Randy Skretka
Randy Skretka

Reputation: 3596

Hey user1010076 I don't know how you got the code from Marty to work ..

stop();
dispatchEvent(
    new Event("movieclip_stopped");
);

I tried and tried and then found this one liner from Introduction to event handling in ActionScript 3.0

target.dispatchEvent(new Event("type"));

So for to have Marty's entire code to work, you would need ..

myMovie.dispatchEvent(new Event("movieclip_stopped"));

I have a similar use case to yours and the code now works with the "target" added whereas I did not without it.

Upvotes: 0

malatze
malatze

Reputation: 21

there's an undocumented feature you could use; addFrameScript(a function of MovieClip); which, as the name suggest, adds code to a certain frame. This way you don't need a framelistener, and you can dynamically add code to a keyframe. So knowing when the movieclip reached its end would be;

mc.addFrameScript(mc.totalFrames - 1, onMcEnded);
function onMcEnded():void {
    trace("last frame of movieclip");
}

if you want to make a movieclip which has certain labels, and then acts on those labels, one way would be to do it like this.(I guess this is pretty common code for anyone who doesn't want to add code directly on the timeline)

let's say you added two labels on the timeline: askUser1 and askUser2

the way to add interaction on those labels would be like so:

for each(var frameLabel:FrameLabel in mc.currentLabels) {
      configureLabel(frameLabel.name,frameLabel.frame-1)                
}

protected function configureLabel(labelName:String,labelFrame:int):void 
{
    switch(labelName) {                 
        case "askUser1" :
                mc.addFrameScript(labelFrame, onAskUser1);
        break;                  
        case "askUser2" :   
                mc.addFrameScript(labelFrame, onAskUser2);
        break;
    }           
}
private function onAskUser1():void{
     //do stuff for askUser1 Label
}
private function onAskUser2():void{
     //do stuff for askUser2 Label
}

Upvotes: 2

N Rohler
N Rohler

Reputation: 4615

Horrendously hacky solution is to listen constantly for onEnterFrame:

var isPlaying:Boolean = false;
var lastFrame:int = 0;
addEventListener(Event.ENTER_FRAME, function(event:Event):void
{
    isPlaying = (lastFrame != currentFrame);
    lastFrame = currentFrame;
});

Better solution is to actually track the state based on your stop() code.

Upvotes: 1

Marty
Marty

Reputation: 39466

Because you have to manually stop a MovieClip via stop() you can easily manually dispatch your own event on the same frame using dispatchEvent()

For example, on a frame you could have:

stop();
dispatchEvent(
    new Event("movieclip_stopped");
);

And the listener for that MovieClip would be:

myMovie.addEventListener("movieclip_stopped", _stopHandler);
function _stopHandler(e:Event):void
{
    trace('mc was stopped');
}

Upvotes: 6

Related Questions