Like in many programming languages, Actionscript 3 allows for use of the ternary operator ?:
Let’s take a normal if statement as an example:
if( score == 10)
win = true;
win = false;
This could be written in a different way using the ternary operator:
win = (score == 10) ? true : false;
So the syntax is: Expression ? statement : statement
If the expression is true, the value of the first statement is returned. Otherwise the value of the 2nd statement is returned.
Use the ternary operator judiciously. I’d recommend using it only with simple expressions and avoid nesting. This can make code very difficult to read!
Although I don’t like nesting, I think that chaining is not too bad if used to make the code easier to read. Here is an example of how the ternary operator can be used to make the code less noisy and more compact:
x = (height < 100) ? "Short" :
(height < 200) ? "Average" :
(height < 300) ? "Tall" :
(height < 400) ? "Giant";
// The above is equivalent to the following if/else chain
if (height < 100)
x = "Short";
else if (height < 200)
x = "Average";
else if (height < 300)
x = "Tall";
x = "Giant";
To be fair, I could have written each of those if/else statements on one line each, but I still feel like the ternary version is cleaner. Of course, it is completely up to you where and when you use it. Enjoy!
This one has been covered a million times on other sites but I thought I’d include it here anyway.
To move a MovieClip or other DisplayObject to the front of the stack, you can use the following function:
public function front(asset:DisplayObject):void
asset.parent.setChildIndex(asset,asset.parent.numChildren - 1);
DisplayObject is used since anything that would be added to the display list (added to the stage) is a DisplayObject at its core. I’ve seen this function all over the web in different forms; quite often they only accept MovieClips or even use the * wildcard. This function will universally work with MovieClips, Textfields or any other display object you want to throw at it.
I am often given AI files (Adobe Illustrator) for the purpose of extracting assets for my flash projects. Over the past few months, I’ve been receiving a lot of files having been giving me this same issue.
Basically, any time you switch focus from Illustrator to any other application, such as flash – and then switch back to Illustrator, the program will freeze up for a good 20 to 30 seconds before allowing me to manipulate any objects. I did a few troubleshooting steps, such as starting a blank document and reproducing my steps. The problem would never reproduce with a blank file, or any files that I made. So obviously, in my mind it had to be a problem with the file.
It was time to upgrade, so I just went ahead and upgraded my adobe suite to CC (Creative Cloud). So I proceeded to open the files in Ilustrator CC and the same problem occurred.
So here is the thing – The client had told me to ignore any missing linked files in the illustrator document because they were remnants from reference material. So just for fun, I found the layers with the missing linked images, and deleted them.
Just like magic, the problem was gone. Lesson learned! Just thought I’d pass that on.
As you may have noticed, you can’t iterate over the properties of an Actionscript 3 class as if they were normal properties of a generic object, unless the class is dynamic of course.
here’s what I mean:
public class Widget
public var color:String = "green";
public var shape:String = "round";
public var age:String = "5";
var widget:Widget = new Widget();
for(var i:String in widget)
trace(i); // Whaaaaa? Output is blank!
Ok, so as I said.. it doesn’t work. However, there’s trick that you can use! generate an XML list of the class variables, then access them as attributes (using the above example class):
var widget:Widget = new Widget();
var xmlList:XMLList = flash.utils.describeType(widget)..variable;
var vars:Object = new Object();
for (var n:int; n < xmlList.length(); n++)
vars[xmlList[n]. @ name] = widget[xmlList[n]. @ name];
for (var prop:String in vars)
trace(prop); // lists: age, color. shape
I took it one step further and pushed the variables into an object called vars so that I could show iterating it as an object.
This came in really handy for me earlier today. Maybe later I’ll elaborate, but I hope that you can see the value in being able to do something like this!
Adding a dropshadow effect to a Display Object in Actionscript 3 is really easy. In Actionscript 3, Display Objects such as MovieClips have a property called filters.
To apply a drop shadow to your MovieClip, you must first import the DropShadowFilter. Then create a DropShadowFilter instance. Lastly, assign your new filter to the MovieClip’s filters property:
var dropshadow:DropShadowFilter = new DropShadowFilter(15,45,10,0.5, 10.0, 10.0,1.0);
myDisplayObject.filters = [dropshadow];
I won’t get into the values passed to the DropShadowFilter constructor to make this short and sweet. If you would like to learn more about the particulars of this filter, see: Actionscript 3 Reference Manual