var myButton:SimpleButton = new SimpleButton();
SimpleButton類有4個屬性分別代表按鈕的四個不同狀態:upState,overState,downState和hitAreaState。你可以為每一個狀態創建一個新的顯示對象,然后將顯示對象賦予SimpleButton的各種狀態:
myButton.upState = mySprite1;
myButton.overState = mySprite2;
myButton.downState = mySprite3;
myButton.hitAreaState = mySprite4;
15,數組定義中的逗號
本文非直接翻譯,原文解釋部分如下:
When defining arrays in ActionScript 3 using the shorthand array access operator (brackets), you can now have a trailing comma following the last element without causing an error (like in PHP). This makes working with multi-line array definitions a little less error-prone when rearranging elements.
先來看一個例子:
var myList:Array = [
"The",
"quick",
"brown",
"fox",
];
在AS1和2中,"fox"后的逗號會導致一個編譯錯誤,但是在AS3中不會了。
注意,這個逗號只是在使用[]定義數組的時候有效,使用Array()或new Array()的時候是無效的。
16,包塊
AS3中的包定義方式和AS2中有所不同。在AS3中,包路徑不再是類定義的一部分,而是使用一個包塊來包含類。定義包塊使用的是package標簽,如下:
package my.package.path {
class MyClass {
}
}
而在AS2中,應該是下面的樣式:
// ActionScript 2:
class my.package.path.MyClass {
}
實際上,在AS3中,所有的類都必須定義在包里面,如果一個類不屬于任何一個包,也需要使用空的包路徑來定義:
package {
class NotInAPackageClass {
}
}
每一個包塊可以將一些有關聯的類或者方法包含在一個文件里面。文件中包塊里的類或方法必須使用和文件名相同的名稱:
package com.kirupa.utils {
function StripString(str:String):void {
// ...
}
}
上面的代碼應該保存在一個名稱為StripString.as的文件中,并放在路徑為com/kirupa/utils的文件夾里。
新聞熱點
疑難解答