# How to trigger a function when a movie finish playing?

**URL:** https://forum.kirupa.com/t/how-to-trigger-a-function-when-a-movie-finish-playing/264751
**Category:** flash
**Created:** [June 30, 2008, 3:54am UTC](https://forum.kirupa.com/t/how-to-trigger-a-function-when-a-movie-finish-playing/264751 "2008-06-30T03:54:32Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![alex298](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@alex298](https://forum.kirupa.com/u/alex298)
#### Post date: [June 30, 2008, 3:54am UTC](https://forum.kirupa.com/t/how-to-trigger-a-function-when-a-movie-finish-playing/264751/1 "2008-06-30T03:54:32Z")

</div>

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 &lt; 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

---

<div class="post-metadata">

### Author: ![Pier25](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pier25](https://forum.kirupa.com/u/Pier25)
#### Post date: [June 30, 2008, 8:55am UTC](https://forum.kirupa.com/t/how-to-trigger-a-function-when-a-movie-finish-playing/264751/2 "2008-06-30T08:55:09Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![pensamente](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pensamente/32/1208_2.png) [@pensamente](https://forum.kirupa.com/u/pensamente)
#### Post date: [June 30, 2008, 9:02am UTC](https://forum.kirupa.com/t/how-to-trigger-a-function-when-a-movie-finish-playing/264751/3 "2008-06-30T09:02:10Z")

</div>

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]

---

<div class="post-metadata">

### Author: ![pensamente](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pensamente/32/1208_2.png) [@pensamente](https://forum.kirupa.com/u/pensamente)
#### Post date: [June 30, 2008, 9:11am UTC](https://forum.kirupa.com/t/how-to-trigger-a-function-when-a-movie-finish-playing/264751/4 "2008-06-30T09:11:16Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![alex298](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@alex298](https://forum.kirupa.com/u/alex298)
#### Post date: [June 30, 2008, 1:52pm UTC](https://forum.kirupa.com/t/how-to-trigger-a-function-when-a-movie-finish-playing/264751/5 "2008-06-30T13:52:01Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pensamente](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pensamente/32/1208_2.png) [@pensamente](https://forum.kirupa.com/u/pensamente)
#### Post date: [June 30, 2008, 2:03pm UTC](https://forum.kirupa.com/t/how-to-trigger-a-function-when-a-movie-finish-playing/264751/6 "2008-06-30T14:03:59Z")

</div>

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

---

<div class="post-metadata">

### Author: ![alex298](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@alex298](https://forum.kirupa.com/u/alex298)
#### Post date: [July 2, 2008, 2:14pm UTC](https://forum.kirupa.com/t/how-to-trigger-a-function-when-a-movie-finish-playing/264751/7 "2008-07-02T14:14:18Z")

</div>

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
