ActionScript Code Snippets
I only started to learn ActionScript about a year and a half ago when I wanted to participate in Puzzle Pirates’ Grand Crafting Puzzle Project. I tried to learn Java at first, but I didn’t get far. Flash with ActionScript was a lot easier for me since it was much more visual.
So from time to time, I’ll be posting ActionScript Code snippets that I think will be helpful to beginners as well. This is the first entry.
EXAMPLE: Flipping a Card
When the green button is clicked, the card is flipped:
If the movie doesn’t show, click here instead. (Wordpress gets kind of annoyed with Flash on the front page.)
FILE: Click here to download the FLA file.
EXPLANATION:
The card is a MovieClip that has two frames - the front (Two of Spades) and the back. When the green button is clicked the “flipClip” function is called. This starts flipping the card by calling the “flipFront” function which sets up the timer and two listeners.
The first listener (TimerEvent.TIMER, changeXScale) is called on every Timer tick and causes the xScale of the card to decrease by 0.1. There are only 10 ticks so the timer stops when the xScale reaches 0.
The second listener (TimerEvent.TIMER_COMPLETE, flipBack) is called once the Timer has finished its 10 ticks. At this point the card has shrinked horizontally to an invisible line (since the xScale is 0) and “flipBack” function is called.
The “flipBack” function changes the frame of the card clip. If it’s showing Frame 1, it changes to show Frame 2 and vice versa. It also multiplies the “flipTracker” by -1. This number determines whether the card is shrinking or expanding.
Similar to the “flipFront” function, the “flipBack” function creates a new Timer and adds two Listeners to it.
The first Listener is called on every tick and since the flipTracker is now positive, the xScale is increased by 0.1 each tick. This continues until 10 ticks have passed and the xScale is 1.0
The second Listener is called at the end of the Timer and calls the “cleanUp” function. This just removes the listeners so we don’t cause a memory leak.
CODE:
var flipTimer:Timer = new Timer(30, 10);
var flipTracker:int = 1; //Determines whether the card is “shrinking” or “expanding”
flipper_mc.stop();
flip_btn.addEventListener(MouseEvent.CLICK, flipClip); //This it the green button
function flipClip(e:MouseEvent) {
flipFront()
}
function flipFront() {
flipTracker *= -1;
flipTimer = new Timer (30,10)
flipTimer.start()
flipTimer.addEventListener(TimerEvent.TIMER, changeXScale)
flipTimer.addEventListener(TimerEvent.TIMER_COMPLETE, flipBack)
}
function flipBack(e:TimerEvent) {
flipTracker *= -1;
if (flipper_mc.currentFrame == 1) {
flipper_mc.gotoAndStop(2);
} else {
flipper_mc.gotoAndStop(1);
}
flipTimer = new Timer(30,10)
flipTimer.start()
flipTimer.addEventListener(TimerEvent.TIMER, changeXScale)
flipTimer.addEventListener(TimerEvent.TIMER_COMPLETE, cleanUp)
}
function changeXScale(e:TimerEvent) {
flipper_mc.scaleX += 0.10 * flipTracker;
}
function cleanUp(e:TimerEvent) {
flipTimer.removeEventListener(TimerEvent.TIMER, changeXScale)
flipTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, cleanUp)
}




























