Runtime class instantation
When we got the change from Actionscript 2 to Actionscript 3 (in Flash CS3), Adobe left out some new features that got introduced when Flash 8 was released. One of these features was the static function loadBitmap in the BitmapData class. It allowed you to link images in your library to a linkage id and call them in your code to perform any Bitmap action you want. Perfect! But not anymore in AS3 as this function is removed and instead you need to call the image from your library through creating a class reference to the image.
Like this:
-
/*
-
* your image in the library should have the linkage id "image_in_library"
-
*/
-
var image_class:BitmapData = new image_in_library();
Very simple indeed.
But it gets harder when you want to dynamically decide which image you want to load and link a class to the image. Ok, maybe I want to0 much at the same time, but if I could pull this trick the would at least simplify some things for me.
So let's get to work and check out this code:
-
var texture_class:Class = getDefinitionByName(_text_arr[int(getRandom(0, _text_arr.length))]) as Class;
-
var tile:BitmapData = new texture_class(_model.width, _model.height);
Voila. Just to explain you what it does: it loads a random image from the library and instantiates the class that is linked to it. The class itself is just an extended BitmapData class where I hold some information such as a width, height, matrix and some other stuff.
The getDefinitionByName function (found in flash.util.*) comes in very handy as it gives you the class you asked for as a response. But the above code will give you an error telling you it can't find the class you asked for (ReferenceError: Error #1065: Variable XXX is not defined.). Bugger.
Well ok, let's change it a bit:
-
var texture_class:Class = getDefinitionByName("be.proximitybbdo.XXX.visual.textures." + _text_arr[int(getRand(0, _text_arr.length))]) as Class;
-
var tile:BitmapData = new texture_class(_model.width, _model.height);
(You don't need to know for what project this is needed so that's why it is XXX :-).).
What you need to do is guide Flash to the location of the class you want to load dynamically. So, will this piece of code work? No! You will get the same error as before.
One last change is necessary to make it work like you want it to:
-
var _text1:TextureOne;
-
var _text2:TextureTwo;
-
var _text3:TextureThree;
-
-
var texture_class:Class = getDefinitionByName("be.proximitybbdo.XXX.visual.textures." + _text_arr[int(getRand(0, _text_arr.length))]) as Class;
-
var tile:BitmapData = new texture_class(_model.width, _model.height);
From the moment you declare a variable with one of the classes you want to instantiate dynamically, Flash will know that it exists and will execute the code correctly.
But what is all the fuss about?
Can't I just instantiate all the class before and just call the variable name I instantiated it to? Yes of course you can but that way you will load the class directly into the memory of the Flash Play and you may not want this to happen, right?
Example Files
You can download a working example here. It also includes the files that are wrong, just to show you how we got the correct version :). Have fun!