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

本文介绍如何使用InputManager和InputMap进行游戏输入管理。InputManager适用于简单控制,而InputMap则支持更复杂的输入处理,包括第三方设备如手柄,并提供一致的处理方式。文章通过示例代码展示了如何设置输入映射及响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.         <!-- These correspond to the calls to AddBinding above. -->
05.         <GoLeft>LEFT</GoLeft>
06.         <GoRight>RIGHT</GoRight>
07.      </Input>
08.   </component>
09.</entity>
鼠标绑定:

 

 
1.<entity name="MouseInputHandlingEntity">
2.   <component class="MyInputHandlingComponent" name="Input"/>
3.      <Input>
4.         <!-- These correspond to the calls to AddBinding above.  Only GoLeft needs to be bound because MOUSE_X gives negative as well as positive results. -->
5.         <GoLeft>MOUSE_X</GoLeft>
6.      </Input>
7.   </component>
8.</entity>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值