Is it possible to create dynamic embed function in ActionScript3
for example like this
public function embedImage(path:String):Bitmap{
[Embed(source = path, mimeType = "image/png")]
var NewBitmapClass:Class;
var image:Bitmap=new NewBitmapClass();
return image;
}// tried it, it doesnt work
or maybe in some other way, or even if it is at all possible?
Embedded elements are embedded at compile time. You can't dynamically embed something at compile time... If you want to load resources dynamically, use the Loader
.
The closest you can get with the "dynamic" part, is to create a wrapper class, where you define your images, and you can get them as Bitmap later on by an id.
Unfortunately the properties are public, otherwise the hasOwnProperty function doesn't return true. (If someone finds a better way, please let me know)
See below:
package {
import flash.display.Bitmap;
public class DynamicEmbed {
[Embed(source = "../images/cat.jpg")]
public var cat : Class;
[Embed(source = "../images/parrot.jpg")]
public var parrot : Class;
[Embed(source = "../images/pig.jpg")]
public var pig : Class;
[Embed(source = "../images/quail.jpg")]
public var quail : Class;
public function DynamicEmbed() {
}
public function getBitmap(id : String) : Bitmap {
if(hasOwnProperty(id)) {
var bitmap : Bitmap = new this[id]();
return bitmap;
}
return null;
}
}
}
No, embed source is embedded at compile time. You can not embed anything at run time. That's what embed means, embedding during building the swf.