I have several bitmaps in my flash library that I have exported for Actionscript. Now depending on the Flashvars I receive I want to load the corresponding library bitmap.
How do I load a bitmap class dynamically?
I have several bitmaps in my flash library that I have exported for Actionscript. Now depending on the Flashvars I receive I want to load the corresponding library bitmap.
How do I load a bitmap class dynamically?
Basically, to attach bitmap from the library you would do this:
import flash.display.BitmapData;
import flash.display.Bitmap;
var bmp:BitmapData = new ClassNameOfTheBitmap(0, 0);
var img = new Bitmap(bmp);
addChild(img);
But since you don't know the class name, you'll have to create the class dynamically like this:
import flash.display.BitmapData;
import flash.display.Bitmap;
var classNameFromFlashvars:String = "xxx";
var cls:Class = getDefinitionByName(classNameFromFlashvars) as Class;
var bmp:BitmapData = new cls(0, 0) as BitmapData;
var img = new Bitmap(bmp);
addChild(img);
In this case, the class name in the linkage properties of the image would be xxx
.
You just have to use Loader and ApplicationDomain. Other answers and documentation have good examples of those. After loading external swf you just need to use something like this to get your bitmap class:
loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
Edit: added links :)
I have created a class ex. MyClass
public class MyClass extends MovieClip
{
private var testImg:Bitmap = new Bitmap();
public function MyClass()
{}
public function set testImg(value:String):void
{
var cls:Class = getDefinitionByName('team_' + value) as Class;
var bmp:BitmapData = new cls(0, 0);
testImg = new Bitmap(bmp);
addChild(testImg);
}
}
The items in my library are named team_4534 for example.
Does MyClass actually detect all the library assets? Do I not have to import them in some way or say that they exist?
okey, so I found a really ugly way of not getting 'error ReferenceError: Error #1065: Variable team_xxx is not defined'
I made a function in flex with all the different classes from the .swc:
private function logos():void
{
team_25502;
team_25504;
team_25508;
team_25509;
team_25511;
team_25514;
team_25517;
team_25521;
team_25530;
team_25591;
team_66036;
team_66230;
team_85230;
team_89483;
team_89484;
}
If someone have a better idea, and i'm sure someone do, then please post a comment.