Category Archives: ActionScript

Declaring variable type

During FITC this past weekend, I saw a presenter showing code such as:

var x, y:Number;

This innocent looking line may seem like declaring two variables x & y as the Number data type. However, what it actually does is declaring variable x with an undefined data type, and y as a Number.

To test, try this:

x = "";

Test movie, all is fine. No error message.

Now add this line:

y = "";

Test movie, and you’ll see the error message:

‘Type mismatch in assignment statement: found String where Number is required. y = “”;’

So remember to declare variables as intended, such as:

var x:Number;
var y:Number;

Flash Lite 2.0 Nokia S60 Template

Here is a Flash Lite 2.0 template for the Nokia S60 series. Yesterday‘s release doesn’t seem to include this template.

Place it in the “ConfigurationTemplatesGlobal Phones” directory.

For Windows, the default English location is:
C:Program FilesMacromediaFlash 8enConfigurationTemplatesGlobal Phones

For Mac OS X, put the file here:
/Applications/Macromedia Flash 8/Configuration/Templates/Global Phones/

Download it here: http://quantumwave.com/pub/FL2_S60_Template.zip

When this is installed, you can choose it from Global Phones in the Start Page.

ActionScript 3.0 & the Rebel Alliance

My journey along the prototype chain as a Rebel Alliance leader back in the Flash 5/MX days, using cool hacking techniques with my friend __proto__, made things like associating a movieclip with a class possible back then.

It’s been a long time since those hacks were needed – now with ActionScript 3.0, I have mixed feelings seeing my old friend being removed.

On one hand, I haven’t done anything with this friend for years; on the other hand, it’s been a great adventure exploring the deep dark secrets along the chain – an impossible task without this trusted friend.

Of course, these are all silly memories from an old guy who still enjoys adventures. But this time, it’s about other more exciting things, like: getting things done quicker without reinventing the wheel, using GUI for building applications (so almost anyone can do it, haha), and getting results fast. What has the world come to… 8-P

So, the Flex 2 product line is now in alpha, with Flex Framework 2, Flex Builder 2, Flash Player 8.5, ActionScript 3.0, among others new experiments that were just announced by Macromedia.

This journey is getting interesting again, even though we barely just started with Flash 8 and all its new cool features.

Oh, one more thing… Flex Builder 2 is available to everyone today for testing, see you later Sparkle… 😎

Create object from string of class name

Here’s a commonly asked question on how to instaniate an object from the name of a class at runtime:

To instantiate a class from a string of its name at runtime, the class needs to be referenced first. For example:

import MyClass; // optional
var tmp = MyClass;
delete tmp;

// construct the class name anyway you want
var dynamicClass:String = "MyClass";

// instantiate an object
var obj = new (eval(dynamicClass));

A related article I posted in 2002 (using ActionScript 1.0):

Invoking a dynamic class method: http://quantumwave.com/flash/dynamicMethod.html

Attaching movieclip class without library symbol

Even though it is late Friday/early Saturday, I’ve got to write this one down:

Through an internal implementation of ActionScript 2.0’s compiler (which compiles code into ActionScript 1.0), Peter Hall discovered this little trick.

Here’s a shortened (and more obscured) example:

Actor.as:

To test:

import Actor;
attachMovie(Actor.id, "_mc", 1);

This keeps the number of static variables to a minimum (one). Of course, this trick works only if the compiler uses the “__Packages.” prefix to the class path internally, and may not work in future versions of Flash. Nevertheless, it’s a very cool trick.

Case sensitivity

One of the new features in Flash MX 2004 and the new Flash Player 7 is ActionScript case-sensitivity. Case-sensitivity is enforced only if the movie is published for Flash Player 7, and is not related to whether it is ActionScript 1.0 or 2.0.

For example, a Flash MX 2004 movie set to use ActionScript 2.0 and published to Flash Player 6, is not case-sensitive: swfoo and swFOO are the same.

On the other hand, a movie set to use ActionScript 1.0 and published to Flash Player 7 is case-sensitive: swfoo is different from swFOO.

So case-sensitivity is determined by the Flash player setting, not the version of ActionScript used. Thanks to Chafic Kazoun for the tip.

Import statement and code hints

Unlike the include directive, the import statement in ActionScript 2.0 requires a semi-colon at the end for code hints to work.

For example, without the semi-colon, code hints for the Accordion class is not displayed:

import mx.containers.Accordion
var ac:Accordion;
ac.

Code hints now work:

import mx.containers.Accordion;
var ac:Accordion;
ac.

Note that with or without the semi-colon, the class is still imported.

Ultrashock released a new series of Flash MX 2004 tutorials

Ultrashock has just released a whole series of tutorials on Flash MX 2004.

I was asked to write an article on Flash MX 2004 ActionScript 2.0.

Other topics include:

There are a few things I left out of the tutorial because of the little time I had:

Error class
try {} catch() {} throw {} finally {}
New event model
EventDispatcher vs. AsBroadcaster

Auto Format your logic!

I just discovered a pretty serious bug with Auto-Format (MX 2004):

If you run the following code, you’ll get “true 2”:

Now if you auto-format this code, and then run it, you’ll get:

true 2
false 2
false 1

Why? Because Auto-Format turns the code into:

Note the last two trace() statements, they’re moved outside of the else blocks! This is because of the comments at the end of the two else statements (which are now inside the else blocks). So watch where you put your comments! You can try this by auto-formatting mx.events.EventDispatcher.as and see the same (make sure you don’t save it!)

By the way, I always put my curly braces like this, so I haven’t ran into this bug before, until I tried to auto-format EventDispatcher.as:

The lesson of the day is: Don’t put your comments at the end of a statement and use auto-format, or you’ll run into logic changes like this!