ActionScript 3.0 tips, tricks and how tos…

by Alberto González on Aug.02, 2008, under ActionScript, Coding, Tips & Tricks

Today I decided to make some posts about tips, tricks and how to do some useful things in ActionScript 3.0 for Flash CS3 and Flex 3, also for LiveCycle Data Services with Java and PHP.
This happened because I’ve been receiving some technical questions about ActionScript 3.0 in general and Flex integration with other technologies. After that, a friend of mine told me that It’ll be useful that I post those answers and some tips/tricks in my blog.
I’ll try to post once a day about this topics but I can’t promise that I will. So lets start…
I will start with one of the first things I realized that it is more difficult to do than in past versions of ActionScript. This is the generation of class instances when the class name is dynamically generated.
I will create an sample environment.Imagine that you have an application where the graphic elements in this app are created based on user decisions. This example is very common, but… What you really have to do is to create instances of a Class that, at the moment of the application initialization, you still don’t know.
Lets see…You have three classes

BlueButtonOrangeButtonGreenButton

You dont know what type of class instance will be created so you just let it to be dynamically generated. You can do this with a method inside “flash.utils” package named getDefinitionByName.
getDefinitionByName takes a string argument and try to get the whole class reference, so you could create a new instance based on this new class reference.

import flash.utils.getDefinitionByName;var ClassReference:Class = getDefinitionByName("BlueButton") as Class;var newInstance:* = new ClassReference();

You could have the “BlueButton” string inside a variable.Because at compile time you still don’t know what data type will have the “newInstance” variable, I recommend not to specify it yet. You could then get the class name and cast the variable to the correct datatype.
You can get the class name with another function inside the same package. The function name is “getQualifiedClassName”. You can pass an instance reference as the only argument and the function will give you a string with the name of the class that the instance belongs to.
Also, there is another function named “getQualifiedSuperclassName” which acts almost the same as getQualifiedClassName. The only difference is that this function gives you the name of the super class instead of the class.

import flash.utils.getQualifiedClassName;import flash.utils.getQualifiedSuperclassName;trace(getQualifiedClassName(newInstance)); // BlueButtontrace(getQualifiedSuperclassName(newInstance)); // SimpleButton

This is an example. The source code is in the “files” section of this blog.Hope you find it usefull.
Regards


3 Comments for this entry

  • Roman Dorsinville

    Hi, I found this blog article while looking for help with fixing Microsoft Silverlight. I’ve recently changed browsers from Google Chrome to Firefox 3.2. Now I seem to have a problem with loading sites that have Microsoft Silverlight. Everytime I browse website that requires Microsoft Silverlight, the site does not load and I get a “npctrl.dll” error. I cannot seem to find out how to fix it. Any help getting Microsoft Silverlight to work is very appreciated! Thanks

  • Like Central

    Nice post. have you heard about the new ipad jailbreak? random but I was just thinking about it.

  • oluwaseun

    @Roman Dorsinville, I suppose you are using windows, right?Okay you might want to go to Firefox add-ons and install microosft silverlight. Click tools and add-ons, then type in the microosft silverlight. It would take you there and installing is easy. Once you are done close and restart your browser.

Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!