2011-12-13 80 views
0

在AS3中 - 我使用通用按鈕處理程序來處理影片剪輯對象上的點擊事件。在過去的4個小時裏,我一直在嘗試將圖像添加到此影片剪輯(請參閱* * *)對象。AS3將圖像添加到「擴展」影片剪輯

的代碼(我剪切並粘貼了一點,但所有這一切編譯沒有任何錯誤)

btPlay = new mcButtonPlay(this,"ClickMe",GameImage); // GameImage is an BitmapData object 

public class mcButtonPlay extends navigationButtonHandler { 
    public function mcButtonPlay(Parent:MovieClip,Text:String,GameImage:BitmapData) { 
     super(Text); 
     if (GameImage != null) {     
      var ImageBitMap:Bitmap = new Bitmap(GameImage); 
      this.addChild(ImageBitMap); // * * * This doesn’t show 
      Parent.addChild(ImageBitMap); // Works just to test the image 
     }      
    } 
} 

public class navigationButtonHandler extends MovieClip { 
    public function navigationButtonHandler(Text:String) { 
     ChangeButtonTargetText(Text); 
     Parent.addChild(this); 
    } 
} 
+0

不確定你遇到的問題,但你應該看看AS3編碼約定http://opensource.adobe.com/wiki/display/flexsdk/Coding+Conventions圍繞變量和類的大小寫名。 – 2011-12-13 22:09:57

+0

我不認爲這會工作,因爲你不能將參數傳遞給mcButtonPlay,而它擴展了一個擴展了movieclip的類......不會給你一個錯誤? – Eric 2011-12-13 22:26:05

+0

總是要大寫類名和大寫變量名。這些都是強大的慣例。 作爲一種風格,僅爲事件處理程序(即函數)使用後綴「處理程序」。如果您想表示一個MovieClip,請添加後綴_mc。 – 2011-12-14 05:02:41

回答

0

管理通過交換超強修復它和周圍添加子邏輯:

public class mcButtonPlay extends navigationButtonHandler { 
    public function mcButtonPlay(Parent:MovieClip,Text:String,GameImage:BitmapData) { 
     if (GameImage != null) {     
      var ImageBitMap:Bitmap = new Bitmap(GameImage); 
      this.addChild(ImageBitMap); // * * * This doesn’t show 
      Parent.addChild(ImageBitMap); // Works just to test the image 
     }      
     super(Text); 
    } 
} 

不知道爲什麼,但!

0
 this.addChild(ImageBitMap); // * * * This doesn’t show 
     Parent.addChild(ImageBitMap); // Works just to test the image 

有了這個代碼,ImageBitMap是被從'this'中刪除並放入'Parent',所以你永遠不會在'this'中看到它。

刪除第二行,並告訴我它是否仍在工作。

編輯:

您是否將btPlay添加到舞臺或顯示層級?

例如。

btPlay = new mcButtonPlay(this,"ClickMe",GameImage); 
this.addChild(btPlay); 
0
public class navigationButtonHandler extends MovieClip { 
    public function navigationButtonHandler(Text:String) { 
     ChangeButtonTargetText(Text); 
     Parent.addChild(this); //<--------???? 
    } 
} 

其中在從提問複製上面的代碼段確實來自Parent?看起來navigationButtonHandler類永遠不會被添加到舞臺上,因爲它沒有得到Parent?所以擴展類也永遠不會添加到舞臺上,因此如果將它添加到mcButtonPlay類中,它將永遠不會顯示。
您的擴展構造函數會傳遞Parent參數,但基類不會。這對我來說似乎很奇怪,不應該編譯。或者你在幕後做了一些靜態的事情?

然後按照註釋中提到的大小寫方式工作。如果您遵循常規慣例,閱讀和查找錯誤真的更容易!