6 common Actionscript 3.0 mistakes made by beginners
Published on Thu 30 Dec 2010 by Will
Learning Actionscript 3.0 can be fun and rewarding but can also leave some beginners scratching their heads. Flash is good at alerting developers to most problems but there are several nuances which may trip up beginners. Here are 6 common mistakes to look out for when developing in Actionscript 3.0.
Trying to access the stage in your class’ constructor
All DisplayObjects in AS3 have a stage property. BUT this property is null until the DisplayObject has been added to the display tree. What does this mean exactly? A Flash runtime application has a single stage and the display tree can be thought of as a hierarchy of objects attached to the stage either directly or through a parent/child relationship. Adding a DisplayObject as the child of another container will populate the DisplayObject’s stage property if (and only if) the parent object is attached to the display tree itself.
It is for this reason it is not possible for a DisplayObject to access it’s own stage property within it’s constructor as a DisplayObject cannot be added as the child of another object until AFTER it has been constructed. Quite the conundrum.
If your custom DisplayObject needs to access the stage as soon as it’s been constructed you must add an event listener to it’s constructor which listens to the Event.ADDED_TO_STAGE event. As soon as your DisplayObject is added to the display list this event is fired and you are safe to access the stage property (and also remove the event listener if your wish).
Forgetting percentages are between 0 and 1, not 0 and 100
If you are coming to Actionscript 3.0 from using Actionscript 2.0 then you will no doubt encounter this. In AS2, whenever you use a property which requires a percentage style value such as _alpha the valid value range is between 0 and 100. In Actionscript 3.0 percentage ranges are between 0 and 1. So if you’re wondering why your MovieClip is still 100% opaque when you set it’s alpha property to 50 it’s because it needs to be 0.5 in AS3.
Forgetting to embed fonts
This is a common mistake even the most seasoned of Flash developers make. The problem is usually noticed when you test your SWF on a computer other than your own. Non-standard fonts will display correctly if they exist on the machine running the SWF but will revert back to standard system fonts when not found. To avoid this, make sure you embed any fonts you are using in your SWF and most importantly test your SWF on a computer other than your own before deploying!
Forgetting addChild
Wondering why you can’t see that DisplayObject you just instantiated? Another common mistake is simply forgetting to add a newly created DisplayObject as the child of another container on the display list. So if your DisplayObject is mysteriously absent make sure you remembered to add it to the display list!
Security violations
The Flash player has a pretty solid security system built in which blocks cross site scripting by default and can be the cause of confusion for beginners. When the Flash player is instructed to load some external content, be it an image, XML etc. it first needs to assess the source domain of the file. Content being loaded from the same domain as the SWF will load fine, but loading data from an external domain will fail unless a cross domain policy file is found on the external domain.
A cross domain policy file is a specially formatted XML file which the Flash player will look for when a request to load external data is made. If your SWF is located at siteA.com and you attempt to load a file from siteB.com Flash will first attempt to load and parse the cross domain policy from siteB.com/crossdomain.xml. The cross domain policy basically lists domains which it is allowed to serve data to. In this example siteB.com would list siteA.com in it’s policy file.
This is a common pitfall for beginners because Flash will not throw any errors when testing locally and errors will only manifests after uploading to a server online.
For an easy way to generate a crossdomain.xml file check out crossdomainmaker.com.
Variables and constants are case sensitive
Variables in AS3 are case sensitive. For example the variable myrocketship is not the same as myRocketShip. It is considered best practice to name variables in lower case when containing a single word. Multi-word variables should always begin with a lowercase character with the first letter of subsequent words capitalized eg. myRocketShip. It’s also a common practice to prefix private variables with an underscore eg. _myRocketShip. Constants on the other hand should be entirely uppercase. These naming conventions are a good thing to learn as it helps differentiate your variables and constants.
Your mistakes
Got a mistake which tripped you up while learning Actionscript 3.0? Leave a comment below!
Did you enjoy this post?
Why not subscribe via RSS or follow Ahrooga on Twitter for alerts on new posts :)







Comments
The biggest adjustment I had to make was to lose the _ at the beginning of all properties. I still, to this day, find myself occasionally starting to type _alpha or _visible, then remembering that I am in AS3 and having to remember to remove the _
One thing I see a lot on the Kirupa Forums is beginners mixing up the "name" property with the variable name (which is very common when they are trying to create several of the same item) - for example:
for (var i:int = 0; i < numMCs; i++)
{
var mc:MovieClip = new MovieClip();
mc.name = "mc" + i;
mc.x = i * 40;
}
mc0.y = 50; //Error
That, and the related mixing up variables with properties (which often works anyway, since I believe that Flash Professional converts some variables into properties)
In fact, it's actually quite difficult to help beginners until they properly learn how references work - And I'm not sure how to sum that up in a few paragraphs. :P
For those familiar with AS2 here are some of the changes in AS3
AS2 to AS3 - Common DisplayObject Properties
http://tokenposts.blogspot.com/2010/10/actionscript-displayobject-as2-as3.html
AS2 to AS3 - onEnterFrame
http://tokenposts.blogspot.com/2010/10/actionscript-onenterframe-as2-as3.html