Loading an inline image into AS3

Okay, I feel stupid asking this, but I spent all day trying to figure it out with no success.

In AS3, I want to add an image to the library (call it image.png) and instantiate it using just the code.

I managed to instantiate an external image this way

var pLoad:Loader = new Loader();
pLoad.load(new URLRequest("image.png"));
addChild(pLoad);

      

But no matter what I tried, I cannot load the image from the library the same way. How it's done?

Also: I've seen the [embed] syntax, but ideally I would like to avoid hardcoded image names, that is, I want to select an image and thus generate the image name programmatically.

+2


source to share


5 answers


It looks like it works:

var pDef:Class = getDefinitionByName("image.png") as Class;
var _image:BitmapData = new pDef(10, 10);

m_pSprite = new Sprite();           
m_pSprite.graphics.beginBitmapFill(_image);
m_pSprite.graphics.drawRect(0, 0, _image.width, _image.height);
m_pSprite.graphics.endFill();
addChild(m_pSprite)

      



It looks ugly and inappropriately complicated. Any other way?

0


source


Well, first of all you need to open the library properties window and check the "Export to ActionScript" checkbox, then just put a good reference to your object in the text fields of the class, for this example I'll call it MyImage, ok. Now let's get the code.

var ImageClass:Class = getDefinitionByName("MyImage") as Class;
var image:Bitmap = new Bitmap(new ImageClass(575,430));
addChild(image);

      



That he works well here.

+5


source


The Flash library is quite mystical, but if used, it can be a pretty good way to customize Flash workflows ... I'll try to clear it all up:

The library symbols with the Export for ActionScript option are actually compiled as classes. If there is no class file with the same class name, Flash will compile it with the same name that you declare in the Class field. This means that in your case, if the class name is "image.png", it will actually create a class "png" in the "image" package extending BitmapData (of course, it would make more sense to give it a different class name, say proyect.library. MyImage) ... this means you don't need getDefinitionByName, just instantiate it like you would with any other class:

import image.png;
var bmd:BitmapData = new png(0,0); //the dimensions are irrelevant but necessary

      

Then you need a Bitmap instance to add to displayList:

var bitmap:Bitmap = new Bitmap(bmd,"auto", true); //see the docs for the las two args
addChild(bitmap);
//Bitmap is a DisplayObject, so you can apply transformations to it as with any Sprite or MovieClip.

      

All of this applies to any symbol in the library (except graphics) ... let's say you "export for AS" a sound symbol as "project.library.MySound", then you can simply do this:

import proyect.library.MySound;
var sound:Sound = new MySound();
sound.start();

      

If you have a class file with the same name as your library symbol, Flash will try to use it (if it inherits the default base class). You will notice that all of these characters have an editable Base Class field. You can also set your own class there, as long as it inherits from the default base class ... In bitmaps, these are flash.display.BitmapData, sounds flash.media.Sound, fonts are flash.text.Font, movieclips flash.display.MovieClip and etc ... In the case of MovieClips, if you don't have frames, you can also subclass from Sprite.

All of this, while it may seem a little mystical if applied well, can lead to a fairly convenient workflow for developers and developers working with Flash. For example, you can just set up a nice package with all the UI definition and force your designers to use those base classes to build graphics and animations.

+3


source


Improvement is rudimentary if you already know what a class name is, simply:

var image:Bitmap = new Bitmap(new MyImage(0, 0));
addChild(image);

      

Bitmaps can be easily inserted into sprites:

var image:Bitmap = new Bitmap(new MyImage(0, 0));
var sprite:Sprite = new Sprite;
sprite.addChild(image);
addChild(sprite);

      

Then you can transform the sprite as usual.

nb MyImage parameters are width and height, but they are dummy for subclasses, you can pass anything.

+2


source


As per what I found, it is as easy to do in AS3 as it was in previous versions. One method of working is to create a MovieClip to hold the image and then instantiate that MovieClip and add it to the stage. Another method was to create a class for each image to add and then instantiate and add that class to the stage (the steps for which are located here .

There must be better ways to do this. I will continue to search.

+1


source







All Articles