页面生成部分就不介绍了,唯一值得注意的是 TextField 类增加了一个 appendText 方法。以前的
myTxt.text += "your text";
应该写成:
myTxt.appendText("your text");
如果使用老的方法编译器会提示:这招太慢了,试试新的吧。(Appending text to a TextField using += is many times slower than using the TextField.appendText() method.)
EventDispatcher 类的 dispatchEvent 方法只接受一个参数:event:Event。为了在广播事件的同时传递参数,写一个继承Event的类:TestEvent
internal class TestEvent extends Event{
//code here
}
将事件类型声明为一个字符串常量:
public static const TRACE_INOF:String = "traceInfo";
将要传递的参数和事件类型一起放在构造函数里
private var _who:String;
private var _info:String;
public function TestEvent(type:String,who:String,info:String){
super(type);//调用父类 Event 的构造函数
_who = who;
_info = info;
}
广播事件的代码:
public function dispatch(who:String,info:String):void{
dispatchEvent(new TestEvent(TestEvent.TRACE_INOF,who,info));
}
注册监听器:
dispatcher.addEventListener(TestEvent.TRACE_INOF,onTraceInfo);
接收事件和参数:
public function onTraceInfo(event:TestEvent):void{
var traceTxt:TextField =
getChildByName("traceTxt") as TextField;
traceTxt.appendText(event.who+"dispatch:"+event.info);
}
这里需要注意的是 as ,新的类型转换操作符,将 getChildByName() 返回的 DisplayObject 转换为前面声明的类型 TextField 。如果转换失败将返回 null 。官方给的例子:
public var myArray:Array = ["one", "two", "three"];
trace(myArray as Array); // one,two,three
trace(myArray as Number); // null
trace(myArray as int); // null
源文件: event.rar
首先,AS3里function的参数可以有默认值了。
public function TestFunc(){
myFunc();
}
private function myFunc(para1:int=10,para2:String="str"):void{
trace(para1 + " , " + para2); //10 , str
}
AS3里不能给出多余的参数,
public function TestFunc(){
myFunc(2,"3",4);
}
private function myFunc(para1:int,para2:String):void{
trace(para1 + " , " + para2);
}
编译器给出参数不匹配的错误:ArgumentError: Error #1063: Argument count mismatch on TestFunc/TestFunc::myFunc(). Expected 2, got 3.
这样以前那种用 arguments 拿到不固定参数的方法就不能用了。AS3给出一个新的关键字:… (rest) parameter
public function TestFunc(){
myFunc(2,"3",4,"5",true);
}
private function myFunc(para1:int,para2:String,... more):void{
trace(para1 + " , " + para2); //2 , 3
trace(more); //4,5,true
}
在固定的参数后面跟一个 “…” 和一个表达式(如例子中的“more”),“…” 后面所有的参数将被放到以该表达式命名的数组中。注意 “…” 必须是最后一个参数。
如果使用 “…” arguments 就不可用了,自然也就拿不到 arguments.callee(对当前正在执行的函数的引用),所以在确定不使用 callee 的情况下才能用 “…”。
提到 arguments ,arguments.caller 已经被 “remove” 了。要想拿到 caller 需要把调用函数的 callee 作为参数传给被调用函数。官方的例子:
package {
import flash.display.Sprite;
public class ArgumentsExample extends Sprite {
private var count:int = 1;
public function ArgumentsExample() {
firstFunction(true);
}
public function firstFunction(callSecond:Boolean) {
trace(count + ": firstFunction");
if(callSecond) {
secondFunction(arguments.callee);
}
else {
trace("CALLS STOPPED");
}
}
public function secondFunction(caller:Function) {
trace(count + ": secondFunction\n");
count++;
caller(false);
}
}
}
AS3有了按钮类:SimpleButton ,可以为四种状态分别指定不同的 DisplayObject 。但是 SimpleButton 没有继承 DisplayObjectContainer 类,也就是不能给它添加其他的 child 。如果要创建一个带文字的 Button 怎么办?两种方案:
- 方案一:把文字加到每种 state 里。因为 Shape 也没有继承 DisplayObjectContainer 类,要添加文字 state 就要用 Sprite 。优点是每种状态可以有不同的文字颜色、大小、位置等。缺点是不方便改文字内容。
- 方案二:把 SimpleButton 和 TextField 一起放到一个 Sprite 里。这样 SimpleButton 的 state 可以用 Shape 以节省内存空间。优缺点和方案一相反。
创建一个按钮很简单,为它的四种状态分别指定一个 DisplayObject 就可以了:
btn = new SimpleButton();
btn.name = "btn";
btn.downState = new BtnStatusShape2(downColor,w,h);
btn.overState = new BtnStatusShape2(overColor,w,h);
btn.upState = new BtnStatusShape2(upColor,w,h);
btn.hitTestState = btn.upState;
addChild(btn);
注意必须指定 hitTestState ,就是Flash IDE里创建 Button 时的 hit 帧,响应鼠标事件的区域,如果没有它按钮就失去作用了。一般设置它和 upState 一样就可以了。
第二种方案的每种 state 都是一个 Shape(→ DisplayObject → EventDispatcher → Object):
internal class BtnStatusShape2 extends Shape{
public function BtnStatusShape2(bgColor:uint,w:uint,h:uint) {
graphics.lineStyle(1,0x000000,0.8)
graphics.beginFill(bgColor,0.8);
graphics.drawRoundRect(0,0,w,h,8);
graphics.endFill();
}
}
方案一没有什么好说的。方案二如果想让 btn 响应鼠标事件可以重写装载它的 Sprite 的 addEventListener 方法:
public override function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
btn.addEventListener(type,listener);
}
要重写继承自父类的方法必须使用 override 关键字。而且重写的方法必须有父类方法完全相同的参数名称、数量和类型。
当然方案二也可以通过监听 MOUSE_OVER、MOUSE_OUT、CLICK 等 MouseEv


















