Switch Statement in ActionScript 3

Using switch or case statements in place of “if” statements isn’t hard once you understand how the case is determined. Most examples on the web will show you how to write the switch statement but not how to call it.

The switch statement goes inside a function. In this case the function is named “updateText”. Now imagine you have several buttons in your movie. When the user clicks on any of those buttons, that button is the “event.target”

In plain english it goes like this:
If the “event.target” clicked is “button1_btn” then display “case 1″ in the “tBox_txt” text box then “break”.

What’s up with the Break?
This one is real simple. The “break” is required. Without it, every case will be evaluated and result in a fail.

In this example “event.target” is being evaluated.

button1_btn.addEventListener(MouseEvent.CLICK, updateText)
//
function updateText(event:MouseEvent):void {
switch (event.target) {
case “button1_btn” :
tBox_txt.text = “case 1″;
break;
case “button2_btn” :
tBox_txt.text = “case 2″;
break;
case “button3_btn” :
tBox_txt.text = “case 3″;
break;
}
}

Apple “Bans” Use of Adobe Flash CS5 to Build iPhone Apps

“When the elephants fight, the grass suffers!”

I  often say that when referencing political, self serving, chest pounding, pissing contests that get in the way of good ideas or people doing good work.  I, as were many others, was looking forward to the updates in adobe’s new CS5, especially the ability to develop an app using flash and export it as an iPhone app. But after reading this, it’s clear that myself and anyone else with those same ambitions will have to focus our attention on other mobile platforms such as android.

http://www.adobe.com/devnet/devices/demos/

Not happy with Apple right now for such a schoolyard stunt days before CS5 is launched this Monday.

Read more about it on  Mashable

http://mashable.com/2010/04/08/apple-adobe-flash-compiler/

Passing Variables to A Function in ActionScript 3

I have asked and have been asked, “How do I pass a variable to a function when using the addEventListener to call a function in ActionScript 3?”

Passing a variable to a function in ActionScript 2 was a fairly simple task. However, it is not so easy using ActionScript 3. There are a few more things you must do first.

In ActionScript 2, passing a string variable looked like this.

my_btn.onRelease = function(){
doThis("whatever variable you want goes here");
}

function doThis(a:String){
trace(a);
}

In ActionScript 3, this is what I need to do to get the same results.  I initially wrote the example below thinking at some point, I will have several buttons that the user will interact with such as a navigation menu.

var b:Array = [b1_btn, b2_btn, b3_btn];
var a:Array = ["zero","one","two"];

for (var i:int = 0; i<b.length; i++) {

b[i].addEventListener(MouseEvent.CLICK, clickedOne);
function clickedOne(e:MouseEvent):void {
for (var j:int = 0; j<a.length; j++) {
if (e.currentTarget.name == b[j].name) {
doThis(a[j])
}
}
}
}

function doThis(v:String):void {
trace(v);
}

Get the source fla file here.

If you have any questions just drop me a line.

UPDATE!!!

There’s more than one way to solve this problem.

Thanks to kcreation for this solution.

var btns:Array = [b1_btn, b2_btn, b3_btn];
var a:Array = [“zero”,”one”,”two”];

for (var b in btns) {
btns[b].name = b;
btns[b].addEventListener(MouseEvent.CLICK, clickedOne);
}

function clickedOne(e:MouseEvent):void {
var b:int = int(event.currentTarget.name);
doThis(a[b]);
}

function doThis(v:String):void {
trace(v);
}

Thanks to Mykola Bilokonsky for this solution.

var buttons:Array = [b1_btn, b2_btn, b3_btn];

var dict:Dictionary = new Dictionary();
dict[b1_btn] = “String Argument”;
dict[b2_btn] = new MySampleMovieClip();
dict[b3_btn] = 7

for each (var b:MovieClip in buttons) {
b.buttonMode = true;
b.addEventListener(MouseEvent.CLICK, myHandler);
}

function myHandler(e:MouseEvent):void {
var myVariable = dict[e.currentTarget];
}

Tween.onMotion Methods

Pulled these out of the Flash Help section and figured I post them here as a reference.

AS2

Method Description

Tween.continueTo()
Instructs the tweened animation to continue from its current value to a new value.

Tween.fforward()
Forwards the tweened animation directly to the end of the animation.

Tween.nextFrame()
Forwards the tweened animation to the next frame.

Tween.prevFrame()

Directs the tweened animation to the frame previous to the current frame.

Tween.resume()
Resumes a tweened animation from its stopped point in the animation.

Tween.rewind()
Rewinds a tweened animation to the beginning of the tweened animation.

Tween.start()
Starts the tweened animation from the beginning.

Tween.stop()
Stops the tweened animation at its current position.

Tween.toString()
Returns the class name, “[Tween]”.

Tween.yoyo()
Instructs the tweened animation to play in reverse from its last direction of tweened property increments.

Event Description

Tween.onMotionChanged
Event handler; invoked with each change in the tweened object’s property that is being animated.

Tween.onMotionFinished
Event handler; invoked when the Tween object finishes its animation.

Tween.onMotionResumed
Event handler; invoked when the Tween.resume() method is called, causing the tweened animation to resume.

Tween.onMotionStarted
Event handler; invoked when the Tween.start() method is called, causing the tweened animation to start.

Tween.onMotionStopped
Event handler; invoked when the Tween.stop() method is called, causing the tweened animation to stop.

AS3

Method Description

Tween(obj:Object, prop:String, func:Function, begin:Number, finish:Number, duration:Number, useSeconds:Boolean = false)
Creates an instance of the Tween class.

continueTo(finish:Number, duration:Number):void
Instructs the tweened animation to continue tweening from its current animation point to a new finish and duration point.

fforward():void
Forwards the tweened animation directly to the final value of the tweened animation.

nextFrame():void
Forwards the tweened animation to the next frame of an animation that was stopped.

prevFrame():void
Plays the previous frame of the tweened animation from the current stopping point of an animation that was stopped.

resume():void
Resumes the play of a tweened animation that has been stopped.

rewind(t:Number = 0):void
Moves the play of a tweened animation back to its starting value.

stop():void
Stops the play of a tweened animation at its current value.

yoyo():void
Instructs the tweened animation to play in reverse from its last direction of tweened property increments.

Event Description

TweenEvent.motionChange
Indicates that the Tween has changed and the screen has been updated.

TweenEvent.motionFinish
Indicates that the Tween has reached the end and finished.

TweenEvent.motionLoop
Indicates that the Tween has restarted playing from the beginning in looping mode.

TweenEvent.motionResume
Indicates that the Tween has resumed playing after being paused.

TweenEvent.motionStart
Indicates that the motion has started playing.

TweenEvent.motionStop
Indicates that the Tween has been stopped with an explicit call to Tween.stop().

Tween Import : AS2 and AS3

I like to use code to handle basic animations like fades and sliding assets in. When you want to use the Tween class to add scripted motion to your projects, you’ll need to import these first.

Actionscript 2

import mx.transitions.Tween;
import mx.transitions.easing.*;

Actionscript 3

import fl.transitions.Tween;
import fl.transitions.easing.*;

For tween methods click here.

Serenity Now Button

A couple of the girls in the office were having a tough week and kept say “Serenity Now!!!” when their frustration levels were reaching critical.  They borrowed the saying from old Seinfeld episodes and even found a “Serenity Now!” montage on youtube.  Afterwards, instead of yelling out, someone would just play the video. That’s when I decided to make a button they could run on their desktop and hit when need be.

So if you too are having one of those days, feel free to download the zip file.  The zip contains the exe, swf and app file types. I’ll be converting it to an AIR app soon and adding other mood appropriate sayings to the mix.