views:

926

answers:

4

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?

+3  A: 

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.

kkyy
A: 

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 :)

dragonfly
You're assuming he wants to load stuff from an external swf, while he probably just wants to load bitmap from his current swf's library...
kkyy
A: 

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?

it works in my .fla environment, but when i export it as a .SWC to flex and use the MyClass() , I get the error ReferenceError: Error #1065: Variable team_66036 is not defined. at global/flash.utils::getDefinitionByName()It seems that it doesn't find the classes.
May be there is some problem with ApplicationDomains. Are you sure in Flex your swc gets imported to your main application ApplicationDomain? Also your class must be public (obviously).
dragonfly
its under libs in my application, the MyClass is public of course. I can access everything in MyClass, including set testImg function. Is there a way to say "import library" in MyClass?
A: 

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.