Liam Flaherty
Liam Flaherty

Reputation: 337

Load image directly from library without class in Actionscript 3

I have an image in my Flash library (called sun.png) and I want to load this directly into an instance of a Bitmap (or equivalent) without using Actionscript linkage and creating its own class.

I have tried:

var sun:Bitmap = flash.utils.getDefinitionByName("sun.png") as Bitmap;

and:

var sun:Bitmap = this.loaderInfo.applicationDomain.getDefinition("sun.png") as Bitmap;

But neither of these work (I believe these functions are used for loading classes)

How can I do this without creating a class or loading from the filesystem? Or is this even possible?

Upvotes: 0

Views: 2910

Answers (3)

haag
haag

Reputation: 178

what i think you want to do: first, make sure the image file is in the same folder as your FLA file (no need to have it in the library). then you can get the bitmap by writing this:

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.net.URLRequest;

bitmap:Bitmap;
ldr = new Loader ();
ldr.load( new URLRequest ( "sun.png" ));
ldr.contentLoaderInfo.addEventListener ( Event.COMPLETE, onloaded );

function onloaded ( e:Event ):void {
e.target.removeEventListener ( Event.COMPLETE, onloaded );
bmd = new BitmapData ( ldr.width, ldr.height );
bmd.draw ( ldr.content );
bitmap = new Bitmap (bmd);
}

Upvotes: 0

Discipol
Discipol

Reputation: 3157

You can create some aliases in a static object like: You define them once then use the objects as many times as you want. In the case of Bitmaps, after you get the bitmap, use the bitmapdata in a new Bitmap() because you need more different references with that same bitmapData.

class Refs
{
    public static const PNG_0 : Bitmap= flash.utils.getDefinitionByName("com.example.MyImage") as Bitmap;
    public static const PNG_1 : Bitmap= flash.utils.getDefinitionByName("com.example.OtherImage") as Bitmap;
}

Upvotes: 0

shlajin
shlajin

Reputation: 1576

No, it is impossible. getDefinitionByName is just like getDefinition in current ApplicationDomain, except one of them through error instead of returning null. Both of they are use finding only linked symbols. If you don't link the bitmap it will not even be compiled into output flash movie.

Upvotes: 4

Related Questions