2013-06-28 115 views
0

我學習as3,並從adobe在線教程中構建一些練習。我試圖做一個非常簡單的任務,我認爲我的代碼和tutoriala完全一樣,並且它不工作!我只是不能解決它...在課堂中設置文本框但顯示數字?

我想改變按鈕類中的按鈕的文本字段。

該按鈕被稱爲GameButton,該文本字段被稱爲blabel,是經典文本和動態文本。這裏是GameButton類的代碼。而不是像下面顯示「點擊」它只是將標籤更改爲數字1.跟蹤語句正在工作等它正在那裏,但文本沒有通過或什麼。請幫忙!!!

包{

import flash.display.MovieClip; 


public class GameButton extends MovieClip { 


    public function GameButton() { 
     trace("Gamebutton has been created"); 
     this.blabel.text = "Click"; 
     stop(); 

    } 

} 

}

+0

子實例不能由父代實例的構造函數中的代碼訪問,因爲它們在代碼執行時尚未創建。在訪問子進程之前,父進程必須通過代碼創建子實例,或者延遲對回調函數的訪問,該函數偵聽子進程以分派其Event.ADDED_TO_STAGE事件。 http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/MovieClip.html – shaunhusain

回答

0

長期和短期的它是你可以在代碼中創建按鈕,否則你可以嘗試監聽加到階段從父對象即將開展的活動,你'將孩子添加到(也許是舞臺,也許是另一個DisplayObjectContainer)。聽方法的問題是我不知道你怎麼知道哪個孩子剛剛派發了事件而沒有製作一些混亂的代碼。我認爲第一個選項通常更容易,更有意義,唯一需要注意的是,您必須使用x/y座標放置實例或應用scaleX,scaleY來伸展或縮小對象,而不是使用創作工具進行。您仍然可以使用閃光燈的拖放部分找出座標,並建立個人電影剪輯等

上一些代碼少廢話:在Flash Professional放置在舞臺上

package 
{ 
    import flash.display.MovieClip; 


    public class GameButton extends MovieClip { 

     private var blabel:TextField; //This can be any display object class or a class that extends from a display object class (Sprite, MovieClip, MyCustomButton etc.) 

     public function GameButton() { 
      blabel = new TextField(); //The type after new, should be the same, or a sub-class (extension) of the type used in the variable declaration above 
      addChild(blabel); 

      //blabel.x=10; //optional over ten pixels from left 
      //blabel.y=10; //optional down ten pixels from top 
      //blabel.scaleX=.5; //optional half the width 
      //blabel.scaleY=2; //optional 2 times taller 

      trace("Gamebutton has been created"); 
      blabel.text = "Click"; 
      stop(); 

     } 

    } 
}