<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Some Tricky Mistakes/Solutions in ActionScript

Published on
8,999 Points
2,999 Views
Last Modified:
Approved
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.

package
{
    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.
package
{
    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   
            }
            else
            {
                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
Singleton.getInstance();

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.
0
Comment
1 Comment
 

Administrative Comment

by:Eric AKA Netminder
deepanjandas,

Congratulations! Your article has been published.

ericpete
Page Editor
0

Featured Post

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Join & Write a Comment

In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.
Next Article:

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month