<

Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x

Some Tricky Mistakes/Solutions in ActionScript

Published on
8,837 Points
2,837 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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Join & Write a Comment

Suggested Articles

In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
Suggested Courses
Next Article:

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month