アクションScript 3.0学習ノート(1)


もっと読む
1、関数の定義:
function    (  :  ):    {

  //     

}
 
例えば:
function add(a:int,b:int):int{

 return a+b;

}
 
エラーの書き方:
function add(var a:int,var b:int):int{

 return a+b;

}
 
 
2、事件処理
実力名称.addEvent Listener(イベントタイプ、コールバック関数)
例えば:
//     
function goToBai(event:MouseEvent):void{
	var target:URLRequest = new URLRequest("http://www.baidu.com");
	navigateToURL(target);
}
//goHome                 
goHome.addEventListener(MouseEvent.CLICK,goToBai)
舞台にキーボードモニターを追加します.
//keyLocation      ,  shift:   1,   2
function keyDownListener(event:KeyboardEvent){
	trace(event.charCode+"-------"+event.keyLocation);
}

//stage        
this.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownListener);
 
3、クラス定義
//     ,     ,        :import   .  ;
package   {
  public class   {
      public function    (  :  ):    {
         //    
      }
  }
}
 例えば:
package{
	
	public class MyClass{
		public function add(a:int,b:int):int
		{
			return a+b;
		}
		
		public function getTime():String{
			var date:Date = new Date();
			var year = date.fullYear;
			var month = date.month;
			var day = date.day;
			var h = date.hours;
			var min = date.minutes;
			var sec = date.seconds;
			
			return year+" "+convert(month+1)+" "+convert(day)+" "+convert(h)+":"+convert(min)+":"+convert(sec);
		}
		public function convert(num:int):String{
			if(num<10){
				return "0"+num;
			}else{
				return num.toString();
			}
		}
	}
	
}
 4、簡単なタイマーの例:
var myclass:MyClass = new MyClass();
var result = myclass.add(10,29);

//   System.out.println("dd");
trace(result);

trace(myclass.getTime());

function setTimeListener(event:TimerEvent){
	trace(myclass.getTime());
        
       //time            
	time.text=myclass.getTime();
}

//Timer      new Timer(    ,    )
var timer:Timer = new Timer(1000);

//     
timer.addEventListener(TimerEvent.TIMER,setTimeListener);

//Timer  
timer.start();
 5、フルスクリーンモードの切り替え:
//    stage.displayState      ,   null
//        
/*
import fl.controls.LabelButton;
import flash.events.MouseEvent;
import flash.display.StageDisplayState;
*/
/*
            ,             ,      ;
*/
public function drawBtn(stage):void{
			trace(stage);
			var labelBtn:LabelButton = new LabelButton();
			labelBtn.label="    ";
			labelBtn.width=100;
			labelBtn.height=50;
			labelBtn.x=75;
			labelBtn.y=210.5;
			
			labelBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent){
				//trace(this.stage.displayState);
				if(stage.displayState == StageDisplayState.FULL_SCREEN){
					trace("    ");
					stage.displayState = StageDisplayState.NORMAL;
					labelBtn.label="    ";
				}else{
					trace("  ");
					labelBtn.label="    ";
					stage.displayState = StageDisplayState.FULL_SCREEN;
				}																				 	
			});
			stage.addChild(labelBtn);
		}