[3.使用 回调函数]
回调函数也可以说是匿名函数,先看下面的代码:
var myXML:XML = new XML();
myXML.load("xml.xml");
myXML.onLoad = function(sUCcess:Boolean)
{
trace(success);
}
XML.onLoad 是一个回调函数.将匿名函数与特定的事件关联,以创建回调函数.函数将在特定事件发生后调用回调函数.
在自定义类中怎么做呢看下面的示例.
[3.1.示例: CFEventClass 类(简单的示例)]
此示例文档详细:
Example/AS2/events/CFEvent/CFEventClass.as
Example/AS2/events/CFEvent/CFEventExample.as
Example/AS2/events/CFEvent/CFEventExample.xml
Example/AS2/events/CFEvent/CFEventExample.fla
主类:
打开 CFEventClass.as 文档,输入下面的代码:
import mx.utils.Delegate;
//----------------------------------------
class CFEventClass
{
//----------------------------------------
public var tXML:XML;
//定义事件函数.
public var complete:Function;
//----------------------------------------
public function CFEventClass(url:String)
{
this.tXML = new XML();
this.tXML.onLoad = Delegate.create(this, this.XMLonLoad);
this.tXML.load(url);
}
private function XMLonLoad(success:Boolean):Void
{
if (success) {
//执行事件函数.
this.complete();
}
}
//----------------------------------------
}
保存文档,这样我们就创建了一个名为 CFEventClass 的类,拥有一个事件,一个属性,此事件在 XML 文档成功加载后发生.
示例类:
打开 CFEventExample.as 文档,输入下面的代码:
import CFEventClass;
//----------------------------------------
class CFEventExample
{
public function CFEventExample()
{
var ee:CFEventClass = new CFEventClass("CFEventExample.xml");
ee.complete = this.complete;
}
private function complete():Void
{
trace("complete");
}
}
保存文档.
在示例类中我们创建主类的一个实例.来加载 CFEventExample.xml 文档.然后使用自定义函数与 complete 事件关联.
如果要移除事件,请使用下面的方法:
ee.complete = undefined
//或
ee.complete = null;
//或
delete ee.complete;
要加载的 XML 文档:
打开 CFEventExample.xml 文档,随便输入一些内容便可.测试用.
示例 fla 文档:
打开 CFEventExample.fla 文档.将"图层 1"重命名为 Actions.并在第一帧中输入下面的代码:
new CFEventExample();
保存文档.
测试 Flash 文档.在 XML 文档成功加载后会在输出面板中显示以下内容
complete
如需给事件传递参数,在执行事件函数时如下添加参数:
this.complete(参数1, 参数2, 参数N);
在调用事件时:
private function complete(参数1, 参数2, 参数N)
{
trace([参数1, 参数2, 参数N]);
}
下面再看一个示例:
[3.2.示例: CFTimer 类(定时器)]
此示例文档详细:
Classes/AS2/utils/CFTimer.as
Example/AS2/utils/CFTimer/CFTimerExample.as
Example/AS2/utils/CFTimer/CFTimerExample.fla
主类:
打开 CFTimer.as 文档,输入下面的代码:
import mx.utils.Delegate;
//----------------------------------------
class AS2.utils.CFTimer
{
//----------------------------------------
private var _timerID:Number;
private var _delay:Number;
private var _repeatCount:Number;
private var _currentCount:Number = 0;
private var _running:Boolean;
//----------------------------------------
public var timer:Function;
public var timerComplete:Function;
//----------------------------------------
/*
@parameter delay: 延迟,单位毫秒.
@parameter repeatCount: 重复的次数.默认为Infinity(正无穷大);
*/
public function CFTimer(delay:Number, repeatCount:Number)
{
if (isNaN(delay)) {
return;
}
if (isNaN(repeatCount)) {
repeatCount = Infinity;
}
this._delay = delay;
this._repeatCount = repeatCount;
}
//----------------------------------------
public function reset():Void
{
this._currentCount = 0;
this.stop();
}
public function start():Void
{
this._timerID = setInterval(CFDelegate.create(this, this.startTimer), this._delay);
this._running = true
}
public function stop():Void
{
clearInterval(this._timerID);
this._running = false;
}
public function toString():String
{
return "[CFTimer]";
}
//----------------------------------------
private function startTimer():Void
{
this._currentCount++;
this.timer(this._currentCount);
if (this._currentCount == this._repeatCount) {
this.reset();
this.timerComplete();
}
}
//----------------------------------------
public function get delay():Number
{
return this._delay;
}
public function set delay(d:Number):Void
{
this._delay = d;
}
public function get repeatCount():Number
{
return this._repeatCount;
}
public function set repeatCount(r:Number):Void
{
this._repeatCount = r;
}
public function get currentCount():Number
{
return this._currentCount;
}
public function get running():Boolean
{
return this._running;
}
//----------------------------------------
}
保存文档.
此类的详细信息:
构造函数:
public CFTimer(delay:Number, repeatCount:Number)
参数:
delay:Number ---延迟,单位毫秒.
repeatCount:Number ---重复次数.默认为 Infinity(正无穷大);
方法:
public reset():Void ---停止定时.并复位 currentCount .
public start():Void ---开始计时.
public stop():Void ---停止定时.
public toString():String ---返回类名称"[CFTimer]".
属性:
public delay:Number ---延迟,单位毫秒.
public repeatCount:Number ---重复的次数.默认值为 Infinity(正无穷大);
public currentCount:Number [只读] ---当前的次数.当开始计时,此值会递增,直到等于
repeatCount.
public running:Boolean [只读] ---定时器目前的状态,true 表示正在运行, false 表示已停止.
事件:
timer = function(currentCount:Number){} ---每当时间间隔到达 delay 时调用.
timerComplete = function(){} ---当 currentCount 等于 repeatCount 时调用.
示例类:
打开 CFTimerExample.as 文档.输入下面的代码:
import AS2.utils.CFTimer;
//----------------------------------------
class CFTimerExample
{
//----------------------------------------
public function CFTimerExample()
{
var te:CFTimer = new CFTimer(1000, 5);
te.timer = this.timer;
te.timerComplete = this.timerComplete
te.start();
}
private function timer(currentCount:Number):Void
{
trace("timer: " + currentCount);
}
private function timerComplete():Void
{
trace("timerComplete: ");
}
//----------------------------------------
}
保存文档.
这里我们创建 CFTimer 类的一个实例, 时间间隔为 1000 毫秒,重复执行 5 次.然后实现 timer 和 timerComplete 事件.
示例 fla 文档:
打开 CFTimerExample.fla 文档,将"图层 1" 重命名为 Actions.并在第一帧中输入下面的代码:
new CFTimerExample();
测试 Flash 文档.输出面板会陆续显示以下的内容:
timer: 1
timer: 2
timer: 3
timer: 4
timer: 5
timerComplete: