QuickTip: Execute Code After Animaiton
Hey there. Here is a new series of quick, but hopefully useful tips for you.
We start of by executing some code after an animation has run once!
If you are coding on the timeline, then go ahead and do that, no need to be reading this post! (I would NOT recommend doing that – follow MJW’s avoider tutorial instead to get going)
If you’re not coding on the timeline, it’s actually really simple, just take a look at the code:
package { import flash.display.MovieClip; public class MainExecuteAfterAnim extends MovieClip { public var animation:MovieClip; public function MainExecuteAfterAnim ( ) { animation.addFrameScript ( animation.totalFrames - 1, stopAnimation ) ; animation.gotoAndPlay ( 1 ) ; } private function stopAnimation ( ) :void { animation.stop ( ) ; //other code } } } |
The reason i made animation.totalFrames - 1
, is that addFrameScript is for some reason null based. This means it starts counting at 0, while the movieclip starts counting at 1. If you for an example want to add a frame script to frame 1, you need to call addFrameScirpt ( 0, nameOfFunction )
.
I basically made an example of it here (it would be no good if I just ran the code above, so I have added some event listeners and stuff). Click it to play the animation once. When the animation is finished, you can click it again to reset.
Happy coding :)
- QuickTip: Execute Code After Animaiton
- QuickTip: Scrolling Only in SWF, not on Page.
- QuickTip: Managing Saved Data (SharedObject)
- QuickTip: Understanding Right Click Menus (ContextMenu)
- QuickTip: Stopping an Event
- QuickTip: Avoid Firefox popup-blocker When Using navigateToUrl
- QuickTip: Inline If-Then-Else Statement
- QucikTip: Trace an Array With Correct Formatting
Posted on January 13th, 2010 Filed under ActionScript 3, Flash, Tutorial |
Wow. I had no idea this function existed. Huh. I guess you’re going into my google reader now. :)