因为工作需要写了个pk游戏中比较常用到的组合键盘的连招类
给大家共享下:请新建一个名为key_func的as文件拷贝下面的脚本;
classkey_funcextendsMovieClip{
varprop;
varoldVal;
varnewVal;
varmid_val;
varm_obj;
//arr:Array(连招组合的键盘数字),(OBJ:Object对象1,OBJ2:Object对象2。。注意函数调用函数的这两个对象最好都是唯一的)
//tmp:String(一个临时争听mc函数调用时最好也是唯一的),func_:Function(成功完成连招时调用的函数,time:Number招式调用的按键间隔)
functionkey_func(arr:Array,OBJ:Object,OBJ2:Object,tmp:MovieClip,func_:Function,time:Number){
varOBJ=newObject();
varOBJ2=newObject();
OBJ2.key;
OBJ2.what=0;
OBJ.num=0;
OBJ.num2=0;
OBJ.onKeyDown=function(){
if(this.once==undefined){
this.once=1;
if(OBJ2.blo==undefined){
if(Key.getCode()==arr[this.num]){
this.num++;
}
}elseif(Key.getCode()==arr[this.num]&&OBJ2.blo!==undefined){
this.num++;
}elseif(Key.getCode()!=arr[this.num]){
this.num=0;
deleteOBJ2.blo;
}
OBJ2.key=getTimer();
if(this.num>arr.length){
this.num=0;
}
}
};
varcnt=0;
tmp.onEnterFrame=function(){
cnt++;
if(cnt>2){
deleteOBJ.once;
cnt=0;
}
};
OBJ.onKeyUp=function(){
deletethis.once;
};
Key.addListener(OBJ);
watch(OBJ2,OBJ2.key,"key",funcForObj,0);
functionfuncForObj(){
if(OBJ.num>1){
if(arguments[2]-arguments[1]>time||arguments[2]-arguments[1]<0){
OBJ2.blo=true;
OBJ.num=1;
OBJ2.what=0;
}elseif(arguments[2]-arguments[1]<=time){
if(Key.getCode()==arr[OBJ2.what+1]){
OBJ2.what++;
}
if(OBJ2.what==arr.length-1){
func_(prop,oldVal,newVal,mid_val);
OBJ.num=0;
OBJ2.what=0;
}
}
}
}
}
functionwatch(Obj:Object,val,string:String,func:Function,mid_val){
if(Obj.once_run==undefined){
Obj.once_run=1;
if(Obj==undefined){
varObj={};
Obj.val;
}
if(mid_val==undefined){
mid_val=0;
}
varWatcher:Function=function(prop,oldVal,newVal,mid_val){
func(prop,oldVal,newVal,mid_val);
if(newVal=="unwatch"){
m_obj.unwatch();
}
returnnewVal;
};
Obj.watch(string,Watcher,mid_val);
}
}
}
在fla文件的调用方法如下:
arr=[37,40,39];
functionfunc(){
//左-下-右
trace("第一式");
}
arr2=[37,38];
functionfunc_2(){
//左-上
trace("第二式");
}
this.createEmptyMovieClip("mc1",getNextHighestDepth());
this.createEmptyMovieClip("mc2",getNextHighestDepth());
varkey_func1=newkey_func(arr2,ob1,ob2,mc2,func,300);
varkey_func2=newkey_func(arr,ob3,ob4,mc1,func_2,200);