Is it possible to create dynamic embed function?

后端 未结 3 1632
自闭症患者
自闭症患者 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;
        }
    }
    }
    

提交回复
热议问题