2010-08-17 97 views
0

假設我們有下面的類:AS3繼承:load或init事件?

public class ImageButton extends MovieClip 
{ 
    private var bmpNormal:BitmapData; 

    // ------- Properties ------- 
    public function get BmpNormal():BitmapData { return bmpNormal; } 
    public function set BmpNormal(value:BitmapData):void { bmpNormal = value; } 


    public function ImageButton() 
    { 
    } 

    public function Draw() 
    { 
     var bm:Bitmap = new Bitmap(BmpNormal); 
     this.addChild(bm); 
    } 
} 

添加的ImageButton的實例下面的代碼:

var imgBtn:ImageButton = new ImageButton(); 
imgBtn.BmpNormal = new onstageBMPData(0,0); 
imgBtn.Draw(); //<- No need for this line ?? 
this.addChild(imgBtn); 

現在的問題/議題是draw()方法是不真的有必要......必須有類時inited或加載到執行抽獎程序的方式,所以生成的代碼將是:

var imgBtn:ImageButton = new ImageButton(); 
imgBtn.BmpNormal = new onstageBMPData(0,0); 
this.addChild(imgBtn); 

我們TRIE d使用INIT,添加或render事件,但似乎並沒有工作

public function ImageButton() 
    { 
     this.addEventListener(Event.RENDER, onAdded, false, 0, false); 
    } 

回答

1

嘗試Event.ADDED_TO_STAGEEvent.REMOVED_FROM_STAGE當您添加或刪除到舞臺的顯示列表中得到通知。

您可以在ImageButton實例本身中收聽此事件。

public function ImageButton() { 
    addEventListener(Event.ADDED_TO_STAGE,handleAdded); 
    addEventListener(Event.REMOVED_FROM_STAGE,handleRemoved); 
} 

private function handleAdded(e:Event):void { 
} 

private function handleRemoved(e:Event):void { 
} 
+0

這是我正在尋找,謝謝。 – 2010-08-17 14:02:09

1

您可以在二傳手添加draw()方法:由於位圖數據值在二傳手的參數傳遞

public function set BmpNormal(value:BitmapData):void { 
    bmpNormal = value; 
    Draw(); 
    } 

private function Draw() 
{ 
    var bm:Bitmap = new Bitmap(bmpNormal); 
    this.addChild(bm); 
} 

,你並不需要一個事件來調用draw()方法,變量bmpNormal從setter中獲取它的值,然後可以在Draw()函數中使用它來創建一個新的Bitmap實例,然後將其添加到ImageButton中。