Mar 18

Instantiating ActionScript 3.0 classes at runtime, and a Flex compiler trick

Tag: FlexDerek Wischusen @ 3:49 am

I’ve been playing around with dynamically instantiating classes in AS 3.0 a lot lately and I thought I would share a couple of tricks that I’ve come across.

You can instantiate a class at runtime using the getDefinitionByName(name:String):Object method in the flash.utils package. To use this method, you simply have to pass it the name of the class that you want to create.

Here are couple good posts that explain how to use this method:

Both of these posts point out that one of the limitations of this method is that the class that you want to create needs to be compiled into your application and that Flex only compiles in classes that are explicitly referenced in your application. This is obviously a major drawback, since one the main reasons that you would want to use dynamic instantion is to create classes that are not already referenced in you app.

So, I did a little searching and I found that there is a way to instantiate a class at runtime that is not explicitly referenced in your application.

Here’s how:

1. Put the classes that you want to instantiate at runtime in a Flex Library Project.

2. In the project for your application, add the .swc that is generated by the library project to the library path.

3. In the compiler arguments for your application add -include-libraries <path_to_swc>

The -include-libraries compiler argument tells the compiler that you want to load all of the classes in the library, even the ones that are not referenced anywhere.

Once you’ve done this, you will be able to load any of the classes in the library at runtime.

Now for the compiler trick:

If you go into the application that loads the library and create classes that have same name and namespace as the classes in your library project, the compiler will load the classes that are in your main application. This means that the library project only needs to contain empty classes that mirror the classes in your application that you want to instantiate at runtime.

For example, let’s say that you have a class called GetUser that you want to instantiate at runtime. If you follow the instructions from above you will have a Flex Library Project and Flex application that loads the SWC that is generated by the library.

Now, let’s say that you’ve put the AddUser class in the following package in your library project

com.mycomp.myproj.command.AddUser

If you put an AddUser class in the com.mycomp.myproj.command package in your Flex application project, when you instantiate the class at runtime the Flex will create the class that is in your Flex application project.

WARNING: The tricks that I described seem to make Flex very unhappy. If you want to test them out I suggest that you use new projects in a new workspace.

Please let me know if you have any questions about any of this.

3 Responses to “Instantiating ActionScript 3.0 classes at runtime, and a Flex compiler trick”

  1. Matjaz says:

    Hi,

    if you want to load and instantiate a class at run-time try this:
    1) compile a swf with a class you want to instantiate at run-time
    2) load the compiled .swf file into ApplicationDomain.currentDomain
    3) get the loaded class with ApplicationDomain.currentDomain.getDefinition(classPath) as Class

    Here is a class that does this:

    package klikfrikLib.utils.loaders
    {
    import flash.display.*;
    import flash.errors.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.*;
    import flash.display.Loader;
    import mx.controls.Alert;

    public class ClassLoader extends Loader
    {
    public static var CLASS_LOADED:String = “classLoaded”;
    public static var LOAD_ERROR:String = “loadError”;

    public function ClassLoader()
    {
    super()
    contentLoaderInfo.addEventListener(Event.COMPLETE,classLoaded)
    }

    private function classLoaded(ev:Event):void{
    var comEv:Event=new Event(Event.COMPLETE);
    dispatchEvent(comEv);

    }

    public function loadClass(filePath:String, classPath:String):void
    {
    if(ApplicationDomain.currentDomain.hasDefinition(classPath)){
    classLoaded(null)
    //mx.controls.Alert.show(”ClassLoader.loadClass = this class already loaded”);
    }else{
    var request:URLRequest = new URLRequest(filePath);
    var context:LoaderContext = new LoaderContext();
    context.applicationDomain = ApplicationDomain.currentDomain;
    try{
    load(request, context);
    }catch(er:Error){
    mx.controls.Alert.show(”ClassLoader can’t load= “+er.message);
    }
    }
    }

    public function getClass(classPath:String):Class
    {

    try
    {
    return ApplicationDomain.currentDomain.getDefinition(classPath) as Class;
    }
    catch(e:Error)
    {
    throw new IllegalOperationError(classPath + ” definition not found in ” + classPath);
    }
    return null;
    }

    }
    }

    And you use it like this:

    classLoader=new ClassLoader();
    classLoader.addEventListener(Event.COMPLETE, handleClassLoaded);
    classLoader.loadClass(filePathToSwf, classPath);
    private function handleClassLoaded(ev:Event){
    var clazz:Class=classLoader.getClass(classPath)
    var clazzObj=new clazz()
    }

    -Matjaz

  2. Richard Karpinski says:

    I have a wild idea of making the mouse do lots more. I want an entire zoom world to be accessible by just mousing. Rollover into a contained zone written smaller auto-zooms into it exactly to where the text is the natural size that the viewer prefers. Recursively.

    I’m thinking that if the contents of the zoom world are just text and images and maybe a video or two, then this is an almost trivial application of FlexibleRails. I expect to pay for an answer, and more for more detail about how and why.

    Having the content in a database would seem to permit arbitrary rearrangement on the fly without changing the application a whit and thus needing no new compile. Each region has a natural screen size and contains deeper regions without limit. Zooming in and out is automatic. Just mousing does all navigation needed to see the whole site. Then we can perhaps add command gestures, and still no buttons or keys until you are filling in some form. Still nearly trivial to implement in FlexibleRails.

    Is it possible that that is true? Or nearly true?
    Can I pay someone to answer questions like this?

    Richard Karpinski, World Class Nitpicker
    148 Sequoia Circle, Santa Rosa, CA 95401
    dick@cfcl.com Home +1 707-546-6760 Cell +1 707-228-9716

    ps Put (or leave) “nitpicker” in the subject line to get past my spam filters.

  3. Agilan palani says:

    Hi Matjaz,
    I tried the code which you gave. But it doesn’t help. It always gives IllegalOperationError. Iam struck up here. Will you provide the code which works for you?

    Thanks
    Agilan Palani

Leave a Reply

Get viagra
Levitra samples
Propecia generic online
Cialis for sale
Cheap xanax for sale
Buy viagra online uk no prescription
Discount viagra india
Buy viagra in england
Viagra shop online
Levitra price
Cheapest place to buy viagra online
Valium cheapest
Xanax bars effects
Viagra generic
Prescriptions for phentermine
Viagra pharmacy prices
Phentermine hcl without prescription
Buy phentermine online no prescription
Cialis price
Order tramadol online overnight
Viagra professional online
Viagra in the philippines
Propecia generic cost
Viagra online purchase in india
Cheap tramadol cod
Australia viagra online
Purchase viagra online without prescription
Phentermine canada no prescription
Free samples of cialis
Propecia online uk
Viagra dosage information
Cheapest online cialis
Cheap valium online
Viagra to buy
Generic xanax no prescription
Phentermine 37.5 mg
Tramadol online no prescription overnight
Order prednisone no prescription
Buy viagra online in ireland
Cost of viagra 50mg
Tramadol medication
Cialis 10mg side effects
Best price tramadol
Propecia price
Cialis over the counter
Order tramadol cod
Buy pfizer viagra without prescription
Side effects of viagra
Xanax 1mg
Buy tramadol hydrochloride
Xanax 0.5 mg
Xanax 1mg side effects
Cialis purchase online
Buy viagra from canada
Viagra ordering
Propecia cost
Discount viagra online
Viagra canada online
Cialis prescription cost
2.5mg cialis
Low cost levitra
Generic levitra uk
Viagra buy online no prescription
Phentermine cheap online
Buy cialis viagra
Valium without prescription
Order cheap phentermine
Get viagra prescription
Dosage of xanax
Buying xanax online without prescription
Levitra 20mg
Buy xanax 2mg no prescription
Buy valium cheap online
Discount viagra pills
Viagra cheap no prescription
Viagra without prescription uk
Buy generic cialis uk
Viagra 50mg side effects
Generic cialis uk
Phentermine 37.5mg side effects
Buy xanax overnight
Cheap xanax bars
Valium without prescription uk
Prescription viagra canada
Tramadol without prescription overnight delivery
Buy female viagra without prescription
Viagra for sale online
Cheap cialis pills
Cheap 37 5 phentermine
Viagra super active
Best way to buy viagra online
Drug phentermine
Tramadol pharmacy
Buy generic cialis
How to buy phentermine online
Tramadol cheapest
Cheap propecia without prescription
Cheapest generic viagra online
Viagra purchase uk
Buying levitra without prescription
Tramadol no prescription overnight delivery
Ordering propecia from canada
Viagra tablets for sale
Where can i buy viagra without a prescription
Cheap cialis
Cialis side effects
Phentermine online free shipping
Cialis samples canada
Purchase phentermine
Generic viagra online without prescription
Tramadol prescription online
Prescription propecia
Cheap tramadol overnight delivery
Low price viagra
Purchase tramadol without prescription
Phentermine with no prescription
Where to buy cialis online
Dose of xanax
Viagra generic cheap
Cheap generic viagra
Valium pill 10mg
Cialis dosage 20mg
Buy tramadol overnight
Cialis soft tabs online
Order cheap viagra online
Best viagra dose
Xanax bars dosage
Levitra purchase
Phentermine diet pills without prescription
Best viagra alternative
Cialis discount price
Authentic phentermine 37.5
Tramadol free shipping
Viagra pills for sale
Viagra discount coupons
Where can i buy viagra without prescription
Purchase xanax
Buy phentermine online without prescription
Buy tramadol hcl
Generic xanax
Cialis 20mg side effects
2mg xanax no prescription
Phentermine purchase online
Cheap levitra uk
Generic tramadol
Buy viagra online cheap
Phentermine without a prescription
Cialis cialis
Purchase cialis without a prescription
40 mg prednisone side effects
Purchase tramadol online
Overnight xanax delivery
Where can i buy viagra in the uk
Order xanax cod
Buy propecia cheap
Prednisone 20mg side effects
Buy phentermine hcl 37.5 no prescription
Buy generic valium online
Where to buy propecia in canada
Viagra in usa
Blood pressure and prednisone
Free cialis samples
Real phentermine without prescription
Generic cialis tadalafil
Ordering cialis online
Best prices for cialis
Australia viagra prescription
Buy generic xanax no prescription
Propecia price australia
Xanax for sale without prescription
Viagra prescription
Where to buy viagra online
Propecia information
Viagra lowest prices
Levitra online buy
Xanax generic dosage
Buy cheap valium online
Propecia uk pharmacy
Prescription valium
Buying viagra in new zealand
Buying viagra in london
Cheap 100mg viagra
Buy generic valium
Propecia best prices
Where can i buy cialis without a prescription
Cialis online canadian pharmacy
Levitra on sale
Order xanax online
Valium online pharmacy
Xanax with no prescription
Viagra 50 mg online without prescription

Viagra canada prices
Prednisone online
Canada viagra no prescription
Xanax no prescription overnight
Buying viagra online
Valium online uk
Purchase levitra online
Viagra in france
Buy viagra online in australia
Purchase phentermine without prescription
Generic viagra sales
Cheapest levitra
Levitra online
Viagra prescription cost
Valium online overnight
Brand viagra cheap
Buy cheap viagra online uk
Buy valium without prescription uk
Propecia 1mg generic
Phentermine buy uk
Buying cialis online without a prescription
Propecia cheap
Cheap phentermine without prescription
Buy generic phentermine online
Buy cialis in the uk
Cheap generic valium
Buy phentermine no rx
Buy propecia
Phentermine 37.5mg
Generic xanax xr
Valium from india
Get tramadol prescription
Discount viagra usa
Canada viagra
Overnight tramadol no prescription
Fedex tramadol
Where to buy viagra in england
Xanax no prescription required
Cheapest cialis price
Viagra india price
Tramadol without prescription
Buy tramadol cod
Cialis 20mg
Valium generic
Cheap levitra no prescription
Best way to take tramadol
Valium online fast delivery
Propecia generic canada
Propecia uk prices
Buy generic cialis online
Cialis order online
How to buy valium without a prescription
Generic viagra 100mg
Buy xanax canada
Cheap viagra online without prescription
Cialis prices uk
Viagra no prescription online
Propecia ireland