本页面由 Flash 保存计划 从 Internet Archive 存档还原 · 快照 2006-07-08 · 原址 http://www.flash8.net/teach/3592.htm ← Flash 保存计划

[更新]键位设置组件和连招组件[KeyMapper & ComboInput]

作者:drmingdrmer   类型:原创   来源:闪吧

  解决了有时不能更改按钮名的bug。flash的类型转换的问题。
更改了使用的方法,现在不需要在场景中添加km和ci的组件了,只需在库中保留km_ci组件,然后在代码中new出KeyMapper或者ComboInput的对象即可。

使用方法:
点击浏览该文件
组件:
点击浏览该文件

/**
system required:
flash mx 2k4 or later
flash player 6 or later
AS 2
*/

///////////////// KeyMapper class ////////////////
/*
class xp.gameLib.input.KeyMapper extends MovieClip implements xp.events.EventDispatcher_itf
{
public function KeyMapper(b:Object, m:Array);
public function addEventListener(event:String, handler):Void;
public function addMap(m:Array):Number;
public function dispatchEvent(eventObj:Object):Void;
public function dispatchQueue(queueObj:Object, eventObj:Object):Void;
function set enabled(e:Boolean);
function get enabled():Boolean;
public function getButtons():Array;
public function getMap(i:Number):Array;
public function getMappedKey(q:String):Number;
public function removeEventListener(event:String, handler):Void;
public function setButtons(b:Object);
public function switchMap(i:Number);
};
*/

//initialize and set buttons and corresponding key codes
//the keys used here are: w,a,s,d,u,i,j,k;
//**the last 4 keys are not used ’cause there are only 4 buttons defined.
var km:xp.gameLib.input.KeyMapper=new xp.gameLib.input.KeyMapper(["up","down","front","back"],[87, 83, 68, 65, 85, 73, 74, 75]);
/**
OR:
var km:KeyMapper=new KeyMapper("up,down,front,back",[87, 83, 68, 65, 85, 73, 74, 75]);
var km:KeyMapper=new KeyMapper();
*/
//add another button_to_key map
//return an ID for this map
var id=km.addMap([87, 83, 68, 65, 85, 73, 74, 75]);
//enable or disable key mapper.
km.enabled=true;//=false
//get all buttons
var buttons:Array=km.getButtons();
trace(buttons);
//get a map or as default get the current map
var mapA:Array=km.getMap(id);
trace(mapA);
/*
OR:
km.getMap();//to return current
*/
//get key code for a specific button
trace(km.getMappedKey("up"));
//reset buttons,
km.setButtons(["up","down","front","back"]);
//or: km.setButtons("up,down,front,back");
//choose another map
km.switchMap(id);

//make some Object to receive buttons event:
km.addEventListener("button",_root);
_root.button=function(eo){
var re=""
for (i in eo.btn) re+=i+"--"+eo.btn[i]+" ";
trace(re);
}
/*
there are some global value for buttons state
_global._KD = 1;
_global._KU = 2;
_global._KH = 3;
_global._KR = 4;
you can use these value to determine the button states:
KD:key just pressed down
KU:key just released up
KH:key is holding down
KR:key is free
*/
/*
////////////////////////// output: ///////////////////////////////
INFO: buttons set to up,down,front,back
up,down,front,back
87,83,68,65,85,73,74,75
87
INFO: buttons set to up,down,front,back
back--4 front--4 down--4 up--3
back--4 front--4 down--4 up--4
back--4 front--1 down--4 up--4
back--4 front--3 down--4 up--4
back--4 front--3 down--4 up--4
back--4 front--3 down--4 up--4
*/
////////////////////////////////////////////////////// before using this Class,I suggest you’d better read the code & comment through carefully first

/*
////////////////////// ComboInput class /////////
class xp.gameLib.input.ComboInput extends MovieClip
{
public function ComboInput(bf:Number, inputDevice:xp.events.EventDispatcher_itf);
public function addCombo(c:Object, priority:Object, nm:String, io:Object):Number;
public function addComboToState(id, st);
public var addListener:Function;
public function addState(st:String);
function set broadcastType(p:String);
function get broadcastType():String;
public var defaultDelay:Number;
public function getCombo(id:Number):String;
public function listAllState();
function set pFuncName(p:String);
function get pFuncName():String;
static var prio:Object;
public var removeListener:Function;
public function resetBuffer(bf:Number);
function set state(st);
function get state():String;
};
*/
//create new ComboInput Object for combo input test
//1st parameter is buffer size ComboInput used for storing input button data from KeyMapper(or sth else dispatching ’button’ event)
import xp.gameLib.input.*;
var ci:ComboInput=new ComboInput(10,km);
//or :
//var ci:ComboInput=new ComboInput(10);//use addEventListener to listen to KeyMapper’s ’button’ event,see below
//var ci:ComboInput=new ComboInput();//default buffer 10 used here.
//add a combo definition:
var ciId=ci.addCombo("up",0,"jump");//this will trigger jump function when ’up’ button pressed..here we used default set.
//or:
ci.addCombo("up(KD)",0,"jump");//it is the same as above.
ci.addCombo("up(KD)=1-10",0,"jump")//the same as abover.
ci.addCombo("up(KD)=1-5",0,"jump");//after ’up’ button pressed,we can hv ’jump’ function triggered in 5 frames
ci.addCombo("up(KD)=3-5",0,"jump");//we can receive the ’jump’ invoke only 3 frames after ’up’ pressed.
ci.addCombo("back,down,front",0,"shot");//defined a CCW_half_circle action to trigger a ’shot’ function for listener Objects.
ci.addCombo("up","normal","jump");//specify a priority ’normal’,not the 0 used above.
ci.addCombo("up","normal-1","jump");//a bit higher priority than ’normal’,it makes this combo to be find first if more than one combo found
//below is the definition for all priorities:
/*
super:0,
special_high:100,
special:200,
special_low:300,
command_high:400,
command:500,
command_low:600,
normal_high:700,
normal:800,
normal_low:900
*/

ci.addCombo("up","normal","jump",{stateList:"stand,jump"});//you can use this combo in 2 states:’stand’ and ’jump’.
//one ComboInput instance could have many states,and at a time it is in one of them.
//each combo has to assign to one or more states, means this combo could only used in these states.you can change combo set from one to another by simply select another state.
//if you dont specify stateList in the 4th parameter,this combo is add to a default state ’normal’,as the addCombo sentences above do
//about state in ComboInput,see below for more.
ci.addCombo("up","normal","jump",{stateList:["stand","jump"]});//the same as above
ci.addCombo("up","normal","jump",{defaultDelay:5});//specify default delay for input,if no time definition in the combo
//for example,this makes this sentence the same as :
ci.addCombo("up","normal","jump",{stateList:["stand","jump"],defaultDelay:5});//....
ci.addCombo("up(KD)=0-5","normal","jump");
//if you dont specify defaultDelay for absent time define,10 is used
//add an exist combo to a state
ci.addComboToState(ciID,"fly");
//make some Object to receive events
ci.addListener(_root);
_root.jump=function(){
trace("jumped");
}
_root.shot=function(){
trace("shot");
}
//add a state
ci.addState("someState");
//
   责任编辑:uufeng    时间:2005年7月13日

正在加载评论...