AS3类游戏中的键盘管理

AS3类游戏中的键盘管理

当你在FLEX或者AS3里面监听KEY_UP事件的时候,通常会有些问题,比如多个键同时按下,本文的目的正是阐述这些问题并提出解决方案

在游戏中,通常按D向右移动,按A向左移动,但是问题是比如当你按着W希望向上移动(当然可能你的程序并没有在此要求),同时当你释放W键的同时,程序仍然控制向左移动,为了完成这个繁杂的任务,我设计了一个SINGLETON类。这个类保存着一列被按下的键,并且有两个查看KEYDOWN KEYUP的函数。

KEYDOWN函数中,该函数首先查看DOWNKEY数组是否是空,如果为空,那么添加相应的KEYEVENT,这样就能够监听相应的事件了。

KEYUP函数中,通过循环,则剔除相应的鼠标事件......

这里有一个技巧:就是在外面的文件只能够添加事件的监听器,然后在这些监听器中去调用我的MANAGER 单件,技巧就在KEYUP的监听器上,可以做一些检测来确认是否需要回调KEYDOWN函数来确认哪些键仍然被按着......

通常在我编写的游戏中,我仅仅使用W,S,D,A键位然后加上和鼠标的互动,交互也就足够了【这也是玩游戏的惯例,太多了反而不熟:译者注】,通过这样的设计我只是希望的我的鼠标交互要比其他的程序要好,如我所说的,我在KEYUP事件的监听器中去除一些事件监听,或者也就仅仅将速度设置为0就可以了......

我希望能够按D,然后按A后退一点点,然后释放A的同时D仍然按着,然后程序对象依旧朝向前方前进......

KeyboardManagement for Games in Actionscript 3

Posted in February 29th, 2008

by admin in Flex

A solution to multiple keys being held down at once. When you listen to KEY_UP in flex, or actionscript 3.0 there are some issues by acting on the KEY_UP Event. Especially for games in actionscript 3.0.

Application - Source

In the Application, start off by pressing “D” to move right, and “A” to move left. The problem is that when you are holding D to move right, lets say you press “W” to move up (now i don’t have W set up to work in this application, but none the less) the whole point is to have “D” still executing while you lift up back off from “W”.

To accomplish this, I created a keyboardManager Singleton class, the class holds an array of keys that are down (downKeys:Array). and 2 functions that execute on keyUp and keyDown.

On keyDown it takes a look at the downKeys array and see’s if it is empty… if so, then it adds the keyEvent to the array, and then it exits…

On keyUp it loops over the downKeys Array and splices it removing the correct keyEvent out of the array….

There is one trick that I do though… there is an outer document that actually adds the listeners for the keyUp and keyDown, in those listener functions, thats where I call the keyboardManager. The trick is on the keyUp listener, I do some checking to find out if I need to re-call the keyDown functions for the keys that are still down…

Typically in the type of game that I am making, I’m only going to be using W,S,D,A for my keys then adding some mouse interaction later on. I wanted the feel with the keyboard to be better than some of the examples I’ve seen, where the KeyUp Event is where things just get removed or speed gets set back to zero…

I wanted to be able to press “D”, then press “A” to back up a little, and take my finger off of “A” with the original “D” still pressed, and have “D” still be getting called…

Anyway it’s late, and I’m tired (it’s 2:45 am)…

Night…

ps… 360flex kicked ass… Thanks to the crew of guitar hero’ers that made it feel real homey… and for the huge attempts at getting rockband working…. a big thanks to Tom, and John for putting on the conference… it was by far the best conference i’ve been to.

Application - Source

 

 

源文档 <http://www.axelscript.com/2008/02/29/keyboardmanagement-for-games-in-actionscript-3/>

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值