看论坛有许多关于双击事件的贴子,写了一个AS2类放上来供大家参考。
代
码:
importmx.events.EventDispatcher;
classDbClickextendsMovieClip{
privatevarfirstClickTime:Number;
privatevarlastClickTime:Number;
privatevarn:Number=0;
privatevardispatchEvent:Function;
privatestaticvar_mixin1=EventDispatcher.initialize(DbClick.prototype);
functionDbClick(){
}
privatefunctiononLoad():Void{
varonwer:Object=this;
_root.onMouseDown=function(){
onwer.ifDbClick();
}
}
privatefunctionifDbClick():Void{
if(n==0){
n=1;
firstClickTime=getTimer();
}else{
n=0;
lastClickTime=getTimer();
}
//两次点击时间差在300毫秒以内计为双击
if(Math.abs(lastClickTime-firstClickTime)<300){
//trace("doubleclick");
//发出事件
this.dispatchEvent({type:"onDbClick",target:this});
}
}
}
用法:
建立一个空的MC并与该类链接,然后把这个MC放到主场景中,取个名字(本例为myDbClick),在主场景的第一帧加上以下代码:
varmyDbClick:MovieClip;
varmyObj=newObject();
myObj.onDbClick=function(evtObject:Object):Void{
//trace(evtObject.target);
//在此书写你的代码
}
myDbClick.addEventListener("onDbClick",myObj);