3

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?

4
  • What do you mean by bitmap class? Images?
    – dirkgently
    Mar 26, 2009 at 9:08
  • yes bitmap images that I have in my flash library.
    – Niclas Adlertz
    Mar 26, 2009 at 9:18
  • Are you sure you want to store your bitmap files in a flash library (I assume you just mean external swf)? You could just load them directly, without being wrapped into swf.
    – dragonfly
    Mar 26, 2009 at 9:27
  • well yes i actually want them in my library since i want them embedded in some way.
    – Niclas Adlertz
    Mar 26, 2009 at 9:44

5 Answers 5

4

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.

1
  • Dont't forget to import the utility. import flash.utils.getDefinitionByName; Feb 17, 2011 at 3:54
3

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

1
  • 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
    Mar 26, 2009 at 9:45
2

That makes perfect sense. The reason is that the compiler does not include any of those classes at 'compile time' as they are never used, its not clever enough to see that the class names might be dynamically generated at run time. So the published swf does not have any of those classes. However after you even 'mention them' in the code the compiler will include them.

I guess this is not an ideal architecture for dynamic class names.Perhaps they should add some sort of force to compile setting?

0

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?

3
  • 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.
    – Niclas Adlertz
    Mar 26, 2009 at 10:12
  • 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
    Mar 26, 2009 at 10:16
  • 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?
    – Niclas Adlertz
    Mar 26, 2009 at 10:23
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.