1. 程式人生 > >ActionScript 3.0 基本語法

ActionScript 3.0 基本語法

測試環境 Flash CS5; 測試準備: 
1、新建 ActionScript 3.0 工程;
2、在第一幀上點選右鍵 -> 動作, 開啟程式碼輸入視窗;
3、輸入程式碼後, Ctrl+Enter 執行.
測試輸出:
trace(123);
trace("abc");

變數與常量:
var a:int, b:int, c:int;
a = b = c = 6;
trace(a, b, c); //6 6 6

const d:int = 123;
trace(d);

資料型別:
//整形
var iV1:int = -1;
var iV2:int = new int(-2);
var iV3:int = new int("-3"
); trace(iV1); trace(iV2); trace(iV3); //無符號整形 var uV1:uint = 1; var uV2:uint = new uint(2); trace(uV1); trace(uV2); //浮點數 var nV1:Number = 3.14159265; var nV2:Number = new Number(-3.14); trace(nV1); trace(nV2); //布林值 var bV1:Boolean = true; var bV2:Boolean = new Boolean(false); trace(bV1); trace(bV2); //字串
var sV1:String = 'ActionScript'; var sV2:String = new String("萬一的 Delphi 部落格"); trace(sV1); trace(sV2); /* Object */ //Date var dateVal:Date = new Date(2011, 5, 13); trace(dateVal.toString()); //陣列 var sArr1:Array = ["spring", "summer", "autumn", "winter"]; var sArr2:Array = new Array("spring", "summer"
, "autumn", "winter"); trace(sArr1[0]); trace(sArr2[1]); var iArr = [11, 22, 33]; trace(iArr[iArr.length-1]); //XML var xmlVal:XML = <List><item>AAA</item><item>BBB</item></List> trace(xmlVal.item[0]); //AAA

資料型別預設值:
//整形
var i:int;
trace(i); //0
trace(int.MIN_VALUE, int.MAX_VALUE); //-2147483648 2147483647

//無符號整形
var u:uint;
trace(u); //0
trace(uint.MIN_VALUE, uint.MAX_VALUE); //0 4294967295

//浮點數
var n:Number;
trace(n); //NaN
trace(Number.MIN_VALUE, Number.MAX_VALUE); //4.9406564584124654e-324 1.79769313486231e+308

//布林值
var b:Boolean;
trace(b); //false

//字串
var s:String;
trace(s); //null

//Object
var obj:Object;
trace(obj); //null

var dt:Date;
trace(dt); //null

//未宣告
var arr:Array = [11, 22];
trace(arr[1]); //22
trace(arr[2]); //undefined

var val;
trace(val);  //undefined

trace(void); //undefined

型別轉換:
//
var i:int;
i = int(true);  trace(i); //1
i = int(false); trace(i); //0
i = int(5.67);  trace(i); //5
i = int(0x1A);  trace(i); //26

i = parseInt('3'); trace(i); //3
i = int("4");      trace(i); //4

//
var n:Number;
n = parseFloat("3.14159265"); trace(n); //3.14159265
n = Number("3.14159265");    trace(n); //3.14159265


//
var b:Boolean;
b = Boolean(-1);   trace(b); //true
b = Boolean(0);    trace(b); //false
b = Boolean(1);    trace(b); //true
b = Boolean(2);    trace(b); //true

b = Boolean("");   trace(b); //false
b = Boolean(" ");  trace(b); //true

var obj1:Object;
var obj2:Object = new Object();
b = Boolean(obj1);   trace(b); //false
b = Boolean(obj2);   trace(b); //true

//
var str:String;
var arr:Array = ["primary", "secondary", "tertiary"];
str = String(arr); trace(str);     //primary,secondary,tertiary
str = String(Math.PI); trace(str); //3.141592653589793

運算子:
[]       //初始化陣列
{x:y}    //初始化物件
()       //對錶達式進行分組
f(x)     //呼叫函式
new      //呼叫建構函式
x.y x[y] //訪問屬性
<></>    //初始化 XMLList 物件 (E4X)
@        //訪問屬性 (E4X)
::       //限定名稱 (E4X)
..       //訪問子級 XML 元素 (E4X)
++       //遞增(字首、字尾)
--       //遞減(字首、字尾)
+        //一元 +
-        //一元 - (非)
!        //邏輯 NOT
~        //按位 NOT
delete   //刪除屬性
typeof   //返回型別資訊
void     //返回未定義值
*        //乘
/        //除
%        //求模
+        //加
-        //減
<<       //按位左移位
>>       //按位右移位
>>>      //按位無符號右移位
<        //小於
>        //大於
<=       //小於或等於
>=       //大於或等於
as       //檢查資料型別
in       //檢查物件屬性
instanceof //檢查原型鏈
is       //檢查資料型別
==       //等於
!=       //不等於
===      //全等
!==      //不全等
&        //按位 AND
^        //按位 XOR
|        //按位 OR
&&       //邏輯 AND
||       //邏輯 OR
?:       //三目運算子
=        //賦值
*=       //乘法賦值
/=       //除法賦值
%=       //求模賦值
+=       //加法賦值
-=       //減法賦值
<<=      //按位左移位賦值
>>=      //按位右移位賦值
>>>=     //按位無符號右移位賦值
&=       //按位 AND 賦值
^=       //按位 XOR 賦值
|=       //按位 OR 賦值

語句:
//if 語句
if () { } else { }

//三目
var a:int, b:int, c:int;
a = 1;
b = 2;
c = (a > b) ? a : b;
trace(c); //2

//switch 語句
var dt:Date = new Date();
var day:uint = dt.getDay();
switch(day)
{
    case 0:
    trace("Sunday");
    break;
    case 1:
    trace("Monday");
    break;
    case 2:
    trace("Tuesday");
    break;
    case 3:
    trace("Wednesday");
    break;
    case 4:
    trace("Thursday");
    break;
    case 5:
    trace("Friday");
    break;
    case 6:
    trace("Saturday");
    break;
    default:
    trace("Out of range");
    break;
}

//for 迴圈
for (var i:int = 0; i < 5; i++)
{
    trace(i);
}

//for in 迴圈
var arr:Array = ["one", "two", "three"];
for (var s:String in arr)
{
    trace(s, ":", arr[s]);
}
/***********
0 : one
1 : two
2 : three
************/
var obj:Object = {x:20, y:30};
for (var s:String in obj)
{
    trace(s, ":", obj[s]);
}
/***********
y : 30
x : 20
************/

//for each 迴圈
var arr:Array = ["one", "two", "three"];
for each (var str in arr)
{
    trace(str); //one/two/three
}

var obj:Object = {x:20, y:30};
for each (var num in obj)
{
    trace(num); //20/30
}

var myXML:XML = <users><fname>Jane</fname><fname>Susan</fname><fname>John</fname></users>;
for each (var item in myXML.fname)
{
    trace(item); //Jane/Susan/John
}

//while 迴圈與 do while 迴圈
var i:int;

i = 0;
while (i<5)
{
    trace(i); //0/1/2/3/4
    i++;
}

i = 5;
do
{
    trace(i); //5
    i++;
} while(i<5);

函式:
//函式語句
function Add(a:int, b:int):int
{
    return a + b;
}
trace(Add(1, 2)); //3
//----------------------------------------------------------------


//函式表示式
var traceParameter:Function = function (str:String)
{
    trace("Hello " + str);
}; //分號
traceParameter("ActionScript"); //Hello ActionScript
//----------------------------------------------------------------


//函式成員
function Sum(a:int, b:int):int { return a+b; }
var arr:Array = new Array();
arr[0] = Sum;
arr[1] = function(a:int, b:int) { return a*b; };

trace(arr[0](3, 4)); //7
trace(arr[1](3, 4)); //12
//----------------------------------------------------------------


//巢狀函式
function myFun():String
{
    function Fun1():String { return "Hello"; }
    function Fun2():String { return "ActionScript"; }
    return Fun1() + " " + Fun2();
}
trace(myFun()); //Hello ActionScript
//----------------------------------------------------------------


//預設引數
function Sum(a:int, b:int=2, c:int=3):int { return a+b+c; }
trace(Sum(1)); //6
//----------------------------------------------------------------


//基本資料型別引數(Boolean、Number、int、uint、String)按值傳遞
function Inc1(a:int):void 
{
    a++; 
}
var b:int = 1;
Inc1(b);
trace(b); //1

//物件引數按引用傳遞
function Inc2(aObj:Object):void
{
    aObj.x++;
    aObj.y++;
}
var obj:Object = { x:1, y:2 };
Inc2(obj);
trace(obj.x, obj.y); //2 3
//----------------------------------------------------------------


//函式中的 arguments 物件
function myFun(a:int, b:int, c:int):void
{
    for (var i:uint = 0; i< arguments.length; i++)
    {
        trace(arguments[i]);
    }
}
myFun(11, 22, 33); //11/22/33

//arguments.callee 表示函式本身; 使用回撥完成階乘的例子:
var factorial:Function = function (x:uint)
{
    if(x == 0)
    {
        return 1;
    }
    else
    {
        return (x * arguments.callee(x - 1)); //同 return (x * factorial(x - 1));
    }
}

trace(factorial(5)); //120
//----------------------------------------------------------------


//... (rest) 陣列引數:
function myFun(...myArgs):void
{
    for (var i:uint = 0; i < myArgs.length; i++)
    {
        trace(myArgs[i]);
    }
}

myFun(1, 2, 3); //1/2/3
myFun("A", "B", "C", 1, 2, 3); //A/B/C/1/2/3

錯誤處理:
try 
{
    throw new Error("丟擲錯誤");
    trace("不會執行");
} 
catch(e:Error)
{
    trace(e.message);
}
finally 
{
    trace("是否發生錯誤都會執行");
}