在闪吧呆了这么久,从没有发过什么像样的东西,前几天在群里有人问判断双击除了用getTimer外还有没有其它的方法, 心血来潮就写了个方法, 主要是用计时的方法。下面是代码:
class devil.com.btn.original.DoubleClick {
/**
Set up an Object to citing a object(btn or mc ) clicked
*/
private var _clickedObject:Object;
/**
Set up an ID
*/
private var _ID:Number;
/**
Set up a delay time
*/
private var _delayTime:Number = 200;
/**
Record counts obj is clicked
*/
private var _count:Number = 0;
/**
Chageing _delayTime property
*/
public function set delayTime(delay:Number):Void {
_delayTime = delay;
}
public function get delayTime():Number {
return _delayTime;
}
/**
Set up a instance
*/
public function DoubleClick(delayTime:Number) {
this._delayTime = delayTime;
}
/**
When button or movieclip is clicked this click method is called
*/
public function click(obj:Object) {
if (this._clickedObject == undefined) {
_clickedObject = obj;
}
if (_count == 0) {
//first click
timerStart();
_count = 1;
} else {
this._clickedObject.onDoubleClick();
//双击
_count = 0;
}
}
/**
Timer star
*/
private function timerStart():Void {
clearInterval(_ID);
_ID = setInterval(this, "timerStop", _delayTime);
}
/**
Timer stop
*/
private function timerStop():Void {
clearInterval(_ID);
if (_count == 1) {
_clickedObject.onClick();
//单击
_count = 0;
}
}
}
下面是用法:
import devil.com.btn.original.DoubleClick;
var b:DoubleClick = new DoubleClick(200);
b.onClick = function() {
//单击要执行的函数
trace("单击")
};
b.onDoubleClick = function() {
//单击要执行的函数
trace("单击")
};
b.onDoubleClick=function(){
//双击
trace("双击")
}
b.onRelease=function (){
_root.a.click(this)}