Pages

Monday 13 January 2014

ActionScript 3.0 Animation Function for Clickable Buttons and MovieClips – To and Fro Animation

A very useful function for animation which can be used for clickable button and movieclips. Below is the code for the same.
// function to animate objects viz buttons, callouts and movieclips
function AnimateObj(secs:int, target_mc:MovieClip):void
{
// import all the necessary classes
 import fl.transitions.*;
 import fl.transitions.Tween;
 import fl.transitions.easing.*;
 import flash.events.Event;
 import flash.utils.Timer;
 import flash.events.TimerEvent;
 import flash.events.MouseEvent;
 import flash.display.MovieClip;
// end of importing all the necessary classes

// Define a interval variable and assign it the value of secs
var interval:int = secs;

// Set the TransitionManager and myTween variables
var myTM:TransitionManager;
var myTween:Tween;

// myTimer variable with interval multiplied by milliseconds.
// For example, 1*1000 = 1 sec, 2*1000 = 2 secs and so on.
// In the second parameter the repeat is given as 0 which means the timer function would execute only once
var myTimer:Timer = new Timer(interval * 1000,0);
// We add a EventListener to the myTimer function
myTimer.addEventListener(TimerEvent.TIMER, goAni);
// Start the myTimer function
myTimer.start();

// Define a goAni function
function goAni(evt:TimerEvent):void
{
// set the Tween rotation animation effect
myTween = new Tween(target_mc,"rotation",Elastic.easeOut,0,2,3,true);
// Add a listener on TweenEvent Motion Finish
myTween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
 }
// on Tween Finish function
 function onFinish(evt:TweenEvent):void
 {
// Reverse the Tween animation
  evt.target.yoyo();
}
}

To use this function simply call it with the required number of arguments like this
AnimateObj(4,my_mc);  
 // where 4 is the number of secs and my_mc is the instance name of the movieclip

No comments:

Post a Comment