(转)AS3 Embed

常用來與變數綁定圖片、swf等等..供其他部份使用,比如icon
Bindable也會用到[],主要是綁定數據用..
[swf(...)]設定主swf一些編譯的常數,比如背景色,高跟寬等等。
AS3 嵌入式Embed的用法

以前只知道外部加载文件图片用load,今天发现一个新东西,记录下来以备查阅。外部加载可以用load和Embed方法,他们区别在于

Embed表示编译的时候加载而load表示执行时加载。Embed和load的用法差不多,如果要在编译时加载,用

[Embed(source="picture.jpg")]

 private var Image:Class;

  现在Image类就包含了外部的源,要调用是直接new就行了。注意!!在[Embed(source="picture.jpg")]
后面千万别加分号,否则会报错的哈。

例子:
而在AS工程下...我们一样可以做到这样的功能... 

package {    
    import flash.display.Sprite;    
    import flash.text.*;            
    public class EmbedImageForTextField extends Sprite  
    {    
        [Embed(source="image.png")]    
        private var yellow:Class;    
        public function EmbedImageForTextField()    
        {    
            var t:TextField = new TextField();    
            t.htmlText = "这里显示一张库里的图片<img src='EmbedImageForTextField_yellow'/>";    
            addChild(t);    
        }      
    }    
}  

使用Embed嵌入图片文件后..
我们一样可以使用img标签的src属性来指定嵌入的图片资源..
不过在指定的时候..需要以"所在类名_变量名"的形式来指定..
上例中:
所在类名为EmbedImageForTextField
变量名为yellow
那指定的时候就需要写为"EmbedImageForTextField_yellow"; 

 

package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Bitmap;
public class App extends Sprite
{
[Embed(source="library.swf", symbol="star")]
private var Star:Class;
[Embed(source="library.swf", symbol="square")]
private var Square:Class;
[Embed(source="library.swf", symbol="circle")]
private var Circle:Class;
[Embed(source="picture.jpg")]
private var Picture:Class;
public function App()
{
init();
}
private function init():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
var star:Sprite = new Star();
addChild(star);
star.x = 100;
star.y = 100;
var square:Sprite = new Square();
addChild(square);
square.x = 200;
square.y = 100;
var circle:Sprite = new Circle();
addChild(circle);
circle.x = 300;
circle.y = 100;
var picture:Bitmap = new Picture();
addChild(picture);
picture.x = 400;
picture.y = 100;
}
}
}
原文地址:https://www.cnblogs.com/hisiqi/p/3059659.html