flex游戏引擎(PushButton)-控制输入(1)

阅读更多

InputManager可以完成一些简单的控制,但是当你要完成较复杂的控制时应使用InputMap,虽然InputManager使用较简单如下:

1. if(InputManager.isKeyDown(InputKey.UP))
2.    moveItemUp();

 

当你有第三方设备输入(比如手柄等),当你要控制键盘和鼠标,你可以采用一种一致的方式去处理它们,这种方式就是使用InputMap,提供一个回调函数,一个输入名称,一个数字参数, Digital inputs (buttons) will be called back with 0 or 1 based on if the button is up or down.用0和1表示键盘的 起来和按下,Analog inputs (mouse) will be called back with the change in position of the input.鼠标使用位置参数。

 

 

 

01. public class MyInputHandlingComponent extends TickedComponent
02. {
03.    public function get input():InputMap
04.    {
05.       return _inputMap;
06.    }
07.      
08.    public function set input(value:InputMap):void
09.    {
10.       _inputMap = value;
11.         
12.       if (_inputMap != null)
13.       {
14.          _inputMap.addBinding("GoLeft", _onLeft);
15.          _inputMap.addBinding("GoRight", onRight);
16.       }
17.    }
18.    public override function onTick(tickRate:Number):void
19.    {
20.       // Normally you would update your position based on this; for simplicity
21.       // we just print the direction we are being indicated to move.
22.       var direction:Number = right - left;
23.       Logger.print(this, "I am moving " + direction);
24.    }
25.      
26.    private function onLeft(value:Number):void
27.    {
28.       left = value;
29.    }
30.    private function onRight(value:Number):void
31.    {
32.       right = value;
33.    }
34.      
35.      
36.    private var _inputMap:InputMap;
37.    private var _left:Number = 0;
38.    private var _right:Number = 0;
39. }
level XML做为实体一部分被启动,注意:你定义输入做为level一部分,更易于支持多人在同一个输入设备游戏
01. <entity name="InputHandlingEntity">
02.    <component class="MyInputHandlingComponent" name="Input"/>
03.       <Input>
04.          
05.          <GoLeft>LEFTGoLeft>
06.          <GoRight>RIGHTGoRight>
07.       Input>
08.    component>
09. entity>
鼠标绑定:

 

 
1. <entity name="MouseInputHandlingEntity">
2.    <component class="MyInputHandlingComponent" name="Input"/>
3.       <Input>
4.          
5.          <GoLeft>MOUSE_XGoLeft>
6.       Input>
7.    component>
8. entity>

你可能感兴趣的:(游戏,Flex,UP,XML)