dispatchEvent应用
dispatchEvent应用

dispatchEvent应用

 dispatchEvent在官方文档中的注释是:将事件调度到事件流中。事件目标是对其调用 dispatchEvent() 方法的 EventDispatcher 对象。
public override function dispatchEvent(event:Event):Boolean
 参数
 event:Event — 调度到事件流中的 Event 对象。 如果正在重新调度事件,则会自动创建此事件的一个克隆。 在调度了事件后,其 target 属性将无法更改,因此您必须创建此事件的一个新副本以能够重新调度。
 返回
 Boolean — 如果成功调度了事件,则值为 true。 值 false 表示失败或对事件调用了 preventDefault()。
 引发
 SecurityError — 调用 Stage 对象的 dispatchEvent() 方法会因任何调用方没有与 Stage 所有者(主 SWF 文件)位于同一个安全沙箱而引发异常。为避免出现这种情况,Stage 的所有者可以通过调用 Security.allowDomain() 方法或 Security.allowInsecureDomain() 方法来向域的调用方授予权限。 有关详细信息,请参阅《ActionScript 3.0 编程》中的”安全性”一章。

以上是官方的原文,没有示例。在学习YouYee 的Simple Video Player时候,有一个dispatchEvent的使用方法。(这一个关于NetStream的dispatchEvent的应用)。

写一个VideoEvent类,继承flash.events.Event数。

package

{

import flash.events.Event;

public class VideoEvent extends Event

{

public static var PLAY:String=”play”;

public var data:*;

public function VideoEvent(type:String,DispatchObj:*=null){

super(type);

if(DispatchObj!=null){

data=DispatchObj;

}

}

}

}

在Simple Video Player中这个类是这样写的(来自YouYee,有删节)。PLAY是一个静态属性,当是public 时,可以通过 VideoEvent.PLAY访问。Super(type); type参数传递给方法的超类版本,或者传递给超类的构造函数。在VideoEvent构造函数中trace(this);可以看到: [Event type=”play” bubbles=false cancelable=false eventPhase=2],在在调用这类时:dispatchEvent(new VideoEvent(VideoEvent.PLAY)); 将事件调度到事件流中,VideoEvent.PLAY 值是VideoEvent类中的PLAY静态值。传给VideoEvent构造函数,type值就是VideoEvent.PLAY,也就是play, DispatchObj值是为null的。也可以带一个值(这是在VideoEvent定义好的,也可以为空)

dispatchEvent(new VideoEvent(VideoEvent.PLAY,”值”));

在加一个侦听器。侦听的对象应该是有引用到dispatchEvent方法和类。如:

simple_video.addEventListener(VideoEvent.PLAY,onVideoPlaying);

simple_video类中有的方法执行dispatchEvent(new VideoEvent(VideoEvent.PLAY)); 当调度成功时返回true。
侦听器simple_video.addEventListener(VideoEvent.PLAY,onVideoPlaying);执行onVideoPlaying,Trace(event);时

[Event type=”play” bubbles=false cancelable=false eventPhase=2],type就是VideoEvent中静态属性PLAY的值。

如果要获取值的话,可以event.data,就可以获得相应的值。data是在VideoEvent定义好的,是通过dispatchEvent载入的。

这样就实现了一个自定侦听事件。

以下为小明补充————————————————————————-
看到群里有人想要了解dispatchEvent.所以google了一下.找到上面的文章.我没仔细看.大家凑合的看. dispatchEvent 用法并不难. 我觉得什么时候用才是关键.用不好的话就是画蛇添足.
什么时候用? adobe自己已经有许多事件了. 大家可以想下adobe 已经定义好的事件有什么特点. 例如一般都会用到的 外部加载swf. 点击,这些事件. 特点是什么时候发生我们不知道,只有对象自己知道. 如果我们想要知道事件发生,就需要监听.不知道大家明白了没. 下面说个我私人类库里的一个类. 我的工作中,常需要批量加载图片, 所以我写了一个专门负责批量加载图片的类 ,首先继承EventDispatcher,然后有一个public函数 load,传个数组进来. 数组都是图片地址.  然后定义了几个事件, 一个是所有图片加载完毕时间 ,一个总进度事件,一个是单个图片加载完成事件.  这个类就这么多内容了. 大家可以试着想写写,稍后我会放出我写的这个类

附帮助文档的例子:
package {
import flash.display.Sprite;
import flash.events.Event;public class EventDispatcherExample extends Sprite {public function EventDispatcherExample() {
var dispatcher:CustomDispatcher = new CustomDispatcher();
dispatcher.addEventListener(CustomDispatcher.ACTION, actionHandler);
dispatcher.doAction();
}private function actionHandler(event:Event):void {
trace(“actionHandler: ” + event);
}
}
}

import flash.events.EventDispatcher;
import flash.events.Event;

class CustomDispatcher extends EventDispatcher {
public static var ACTION:String = “action”;

public function doAction():void {
dispatchEvent(new Event(CustomDispatcher.ACTION));
}
}

 小心得:
用这个函数覆盖官方的函数,会少许提高一点效率
override public function dispatchEvent(evt:Event):Boolean {
if (hasEventListener(evt.type) || evt.bubbles) {
return super.dispatchEvent(evt);
}
return true;
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注