Is it possible to create dynamic embed function?

2019-02-28 09:49发布

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?

3条回答
聊天终结者
2楼-- · 2019-02-28 10:21

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.

查看更多
混吃等死
3楼-- · 2019-02-28 10:37

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.

查看更多
相关推荐>>
4楼-- · 2019-02-28 10:45

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;
    }
}
}
查看更多
登录 后发表回答