How to trigger a function when a movie finish playing?

Hello,

I have a very simple movie. A ball moves in x-axis and stops in a set location. The codes are:

function moveBall(evt:Event):void {
stage.addEventListener(Event.ENTER_FRAME, moveBall);
stage.frameRate = 15;

if (ball_mc.x < 500) {
	tball_mc.x += 1;
} else {
	stage.removeEventListener(Event.ENTER_FRAME, moveBall);
	noticeMe();
        
}

}

moveBall(null);

// trigger noticeMe function when ball_mc stop?

function noticeMe():void{
trace(“The ball is stop”);
}

// I have more codes
// -------- more codes here ---------
// -------- more codes here ---------

// Don’t know how to trigger movieFinish function when the movie finish playing?
xxxxxxxxxxxx.addEventListener(Event.xxxxxxx_COMPLETE, movieFinish);

function movieFinish():void{
trace(“The movie is finish!”);
}

I have a question:

  1. Sometimes I wish to trigger another function when a movie finish playing. How can I do that?

I searched the Internet. I noticed that the COMPLETE is usually works with Event.SOUND_COMPLETE, loader…(Event.COMPLETE,loadImage).

Could you please show me some guidelines?

Thanks and best regards

Alex

Hey alex, you code will not work like that…

you need to put the if inside a function called moveBall

stage.addEventListener(Event.ENTER_FRAME, moveBall);
stage.frameRate = 15;

function moveBall(evt:Event):void {
if (ball_mc.x < 500) {
tball_mc.x += 1;
} else {
stage.removeEventListener(Event.ENTER_FRAME, moveBall);
noticeMe();
}
}

function noticeMe():void{
trace(“The ball is stop”);
}

That should work.

If you want to dosomething when the playhead has reached the last frame you can simply trigger an event or a function by writing some code on that last frame of that movieclip or swf.

hi Alex,

your code seams fine regarding what you need, you increment a value to the x position of a movieclip, and then you you reach a certain point it will stop increment and call a function. Really can’t figure out what do you need after all…

by the way I guess you typed something wrong there:

if (ball_mc.x < 500) {
tball_mc.x += 1;

(ball_mc / tball_mc)

although you could use tween animations so it would set an event for the animation, this way you could listen to a “complete”, that would go something like this:

[AS]import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent

var myTween:Tween = new Tween(ball_mc, “x”, None.easeNone, 0, 500, 3, true);

myTween.addEventListener(TweenEvent.MOTION_FINISH, movieFinish);

function movieFinish (evt:TweenEvent):void
{
trace(evt)
}[/AS]

sorry pier, haven’t see you answer. I tested the code and all seamed ok, just changed the name of tball_mc to ball_mc. When it ends the animation it calls the function… maybe i’m missing the point here…

Hi all,

Thanks for your help.

  1. Sorry, I made a typing mistake (tball_mc should be ball_mc).

  2. Actually my movie has only one frame that use to write all the ActionScript. I am afraid that it cannot trigger an event or a function by writing some code on that last frame.

  3. Yes, tween animations with listen to a “complete” should be what I need. Thanks for your code sample.

Thanks and best regards

Alex

welcome alex.
why don’t you consider using a tween engine? it will spare you some work…

Hi pensamente,

why don’t you consider using a tween engine? it will spare you some work…

Do you mean TweenLite? I tried before.

Thanks and best regards

Alex