看论坛有许多关于双击事件的贴子,写了一个AS2类放上来供大家参考。
演示:http://home.goofar.com/Blog_/flash/DbClick.swf
类名称:DbClick.as
代码:
import mx.events.EventDispatcher;
class DbClick extends MovieClip {
private var firstClickTime:Number;
private var lastClickTime:Number;
private var n:Number = 0;
private var dispatchEvent:Function;
private static var _mixin1 = EventDispatcher.initialize(DbClick.prototype);
function DbClick() {
}
private function onLoad():Void {
var onwer:Object = this;
_root.onMouseDown = function(){
onwer.ifDbClick();
}
}
private function ifDbClick():Void {
if ( n == 0 ){
n = 1;
firstClickTime = getTimer();
}else{
n = 0;
lastClickTime = getTimer();
}
//两次点击时间差在300毫秒以内计为双击
if ( Math.abs(lastClickTime - firstClickTime) < 300 ) {
//trace("double click");
//发出事件
this.dispatchEvent({ type:"onDbClick", target:this });
}
}
} 用法:
建立一个空的MC并与该类链接,然后把这个MC放到主场景中,取个名字(本例为myDbClick),在主场景的第一帧加上以下代码:
var myDbClick:MovieClip;
var myObj = new Object();
myObj.onDbClick = function(evtObject:Object):Void{
//trace(evtObject.target);
//在此书写你的代码
}
myDbClick.addEventListener("onDbClick", myObj);