Is it possible to create dynamic embed function?

后端 未结 3 1593
自闭症患者
自闭症患者 2021-01-23 21:17

Is it possible to create dynamic embed function in ActionScript3

for example like this

     public function embedImage(path:String):Bitmap{
                     


        
相关标签:
3条回答
  • 2021-01-23 21:35

    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;
        }
    }
    }
    
    0 讨论(0)
  • 2021-01-23 21: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.

    0 讨论(0)
  • 2021-01-23 21:40

    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.

    0 讨论(0)
提交回复
热议问题