Some Tricky Mistakes/Solutions in ActionScript

Published on
9,087 Points
Last Modified:
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two.

Here are some of the common mistakes and some quick code tricks and snippets I used, which can help other developers avoid sleep-less nights.

Problem/Solution #1
This is an easy mistake to make when new to AS3.
I set up a for loop, to count backwards through an Array. But for some strange reason the loop was never terminating! It was simple, here's the original code:
var i:uint;
for( i=arr.length-1; i>=0; i-- )

Open in new window

The mistake was simple, I had set i as a uint, so it could never go below 0, so just had to make i an int. It sounds stupid once you realize it, but it wasted enough of my time!

Problem/Solution #2
Once I needed was a strict abstract class in AS3, again, public only constructors, and the absence of the "abstract" keyword from ActionScript meant a workaround.

    import flash.utils.getDefinitionByName;
    import flash.utils.getQualifiedClassName;
    public class AbstractExample
        public function AbstractExample()
	            if( Class( getDefinitionByName( getQualifiedClassName( this ) ) ) == AbstractExample ) throw new Error( 'AbstractExample must be extended' );
	            // rest of constructor here...

Open in new window

Problem/Solution #3
As class constructors in ActionScript 3.0 must be public, there's not a really clean way to force a class to be a singleton as there is in ActionScript 2.0 or Java say.
    public class Singleton
        public static var instance:Singleton;
        public static function getInstance():Singleton
            if( instance == null ) instance = new Singleton( getInstance );
            return instance;        
        public function Singleton( caller:Function )
            if( caller == Singleton.getInstance )
                // instantiate class here   
                throw new Error( 'Singleton is a singleton, use getInstance();' );

Open in new window

It's pretty simple, you can't call new Singleton() because the compiler will be expecting an argument, and if you do pass an argument, it will throw an Error because the caller is not the Singleton.getInstance method.

So you can only access this class by calling

Open in new window

Problem/Solution #4
Copy motion as ActionScript 3.0
If you are a developer, you might have been tasked with changing timeline animation to code. This can be a challenging task, because you need to replicate complex animation using complex code. Making the timeline animation and your code match might be both challenging and time-consuming.

Your work just got easier: Now you only need to select a menu option, and your code is automatically written for you in Flash CS3. Flash copies the properties that define a motion tween that resides on a timeline and writes the ActionScript 3.0 code for you to apply to a symbol. You can choose to apply the code as timeline code or in class files for the FLA file.

Here's how you copy motion as ActionScript 3.0, and apply it to a second symbol.
1. Create a new symbol or group, and place it on the Stage on Layer 1.
2. Animate that instance on Layer 1 using a timeline animation, such as a simple motion tween on Layer 1.
3. Create a new layer (Layer 2).
4. Create a new symbol or group, or drag another instance of your existing symbol to the Stage on Layer 2.
5. Select the entire animation that you want to copy on Layer 1 of the timeline, right-click the animation, and then choose Copy Motion as ActionScript 3.0 from the context menu. The ActionScript code copies to the clipboard.
Tip: Click the first frame of the animation, press the Shift key, and then click the last frame of the animation to select a span of frames.
6. Type an instance name to use in the ActionScript code. You want to enter the instance name of the instance you will apply the ActionScript code to.
7. Select your second instance that you added to Layer 2, and type the instance name you entered into the Property inspector.
8. Create a new layer (Layer 3), and select the first frame of that layer. Open the Actions panel and paste (Control + V or Command + V) the code into the Script pane.
9. Before you test your animation, you need to make sure that Layer 2 has the same number of frames (so the symbol doesn't disappear when the animation plays). Select a frame and press F5 to insert frames on the Timeline.
10. Select Control > Test Movie to play the animation. You successfully applied the motion tween from Layer 1 to the instance on Layer 2 using ActionScript code. The scripted animation, and the one using timeline animation, should match.

For video tutorials about copying motion as ActionScript 3.0 code, see Copying and pasting ActionScript from an animation and Creating an effective workflow between design and development.
Enjoy this complimentary article view.

Get unlimited access to our entire library of technical procedures, guides, and tutorials written by certified industry professionals.

Get 7 days free
Click here to view the full article

Using this article for work? Experts Exchange can benefit your whole team.

Learn More
Experts Exchange is a tech solutions provider where users receive personalized tech help from vetted certified professionals. These industry professionals also write and publish relevant articles on our site.
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Learn from the best.