[Flash&ActionScript]ActionScript筆記-宣告變數(var myButton:Button = new Button());範例:動態建立Button

[Flash&ActionScript]ActionScript筆記-宣告變數(var myButton:Button = new Button());範例:動態建立Button

imageimage

(實作)

image(在畫面拉一個BUTTON)image(設定屬性)

image

image

(程式碼)

   1:  import flash.events.MouseEvent;
   2:  import flash.net.URLRequest;
   3:  import flash.net.navigateToURL;
   4:  import fl.controls.Button;
   5:   
   6:  function CreateAButton(event:MouseEvent):void
   7:  {
   8:      //宣告Button實體myButton,並定義myButton的屬性
   9:      var myButton:Button = new Button();
  10:      myButton.width = 100;
  11:      myButton.height = 20;
  12:      myButton.x = Math.random() * 400;
  13:      myButton.y = Math.random() * 400;
  14:      myButton.label = "hit Me";
  15:      myButton.addEventListener(MouseEvent.CLICK,CreateAButton);
  16:      this.addChild(myButton);
  17:   
  18:  }
  19:  //為畫面上的button加入事件傾聽者:滑鼠點擊時,呼叫-方法CreateAButton
  20:  btnNewButton.addEventListener(MouseEvent.CLICK,CreateAButton);

※var 宣告變數 例:var i : Number = 10;

基本資料類型:String(文字)、Number(有小數或沒小數的值)、int(整數)、uint(unsigned-int無正負號整數)