XNA处理按键组合 [英] XNA Handling a Combination of key presses

查看:83
本文介绍了XNA处理按键组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些有关此问题的文章,但是在尝试实现一些问题后,总是会遇到同样的问题,

I have read though a a few articles dealing with this however after attempting to implement a few, a always get the same issue,

快速注释:这是家庭作业

我正在使用XNA为我的游戏设计类重新创建Super Smash Brothers(N64风格). 现在我有2个密切相关的问题,任何熟悉SSB的人都会知道连击,即按"Forward A","Up B"等.我有一个正在使用的Yoshi,您有时甚至可以取消他的连击,但是我发现的主要问题是,除非在下一个动作的同一时刻(例如,对于上A键)同时按下按键,否则跳动将消失

I'm recreating Super Smash Brothers(N64 style) for my game design class, using XNA. now i have 2 issues that are closely related anyone familiar with SSB will know of combo attacks i.e. press "Forward A", "Up B", etc. i have a currently working Yoshi and you can sometimes even get his combos off, however the main issue I'm finding is that unless the keys are pressed at the exact same moment another move i.e. for Up A, jump will go off

目前,我的if语句是....

currently my if statements are....

if (state.IsKeyDown(Keys.Up) && state.IsKeyDown(Keys.B))
{
  actionComponent.curAction.secondaryAction = SecondaryAction.Stand;
  actionComponent.curAction.primaryAction = PrimaryAction.Up_B; 
}
else if (state.IsKeyDown(Keys.Up) && state.IsKeyDown(Keys.A))
{
  //Console.WriteLine("This is C#");
  actionComponent.curAction.primaryAction = PrimaryAction.Up_A;
  actionComponent.curAction.secondaryAction = SecondaryAction.Smash;
  spriteComponent.curColumn = 0;
}
else if (state.IsKeyDown(Keys.Up))
{
  actionComponent.curAction.secondaryAction = SecondaryAction.Jump;
  actionComponent.curAction.primaryAction = PrimaryAction.None;
} 

我显然为每个可能的动作还有很多,但它们的排列方式都是使最后一次按键有效,因此可以注册其他按键,但是除非我同时按下它们,否则它不会注册按键-按 -我已经尝试过的一些想法,在if语句中处理箭头键,使线程休眠一小会儿,然后在箭头键和按钮(即A被按下)时暂停操作)

i obviously have many more for each possible action but they are all arranged so that the single key press is the last one so its possible to register the others, but unless i press them at the same moment it wont register the key-press - a few ideas Ive tried, handle the arrow key in an if statement, sleep the thread for a short moment, then chick if the arrow key and button i.e. A is pressed)

我的第二个问题,我正在讨论实施的问题,但是如果我这样做的话,SSB玩家会知道在操纵杆上稍稍向前推,然后按下A会快速踢/刺,但按键会更硬(操纵杆移得更远/也许更快的w/e)将创造出扣人心弦的想法,并且提出了一些想法,即如何实现一个可以识别轻按键和重按键的系统?

My Second Problem, I'm debating of implementing but if i do then and SSB player will know slightly pressing forward on a joystick then pressing A will give a quick kick/Jab but a harder key press(joystick moves farther/maybe faster w/e) will create a smash move, and ideas how i can implement a system to recognize between a light and heavy keypress?

推荐答案

您的问题是XNA默认为60Hz循环,因此如果您的游戏时钟足够快,则每16毫秒检查一次按键.这为您提供了一个小小的窗口,可以同时按下两个键-如果两个键的间隔时间相差超过10毫秒,则它们可能最终会出现在两个单独的更新循环中,因此被计为单个按键,而不是组合移动.人类的反应时间约为150-300毫秒,因此您完全可以避免人工输入中的误差.

Your problem is that XNA defaults to a 60Hz loop so if your game is clocking fast enough then you're checking for keypresses every 16 milliseconds. That gives you a veeery small window in which to hit both keys - if the two keys go down more than about 10 milliseconds apart, then they'll probably end up in two separate update loops and thus get counted as single keypresses rather than combo moves. With human reaction times of around 150-300ms, you're well within the inaccuracy you'll get in human inputs.

我可能会考虑使用输入缓冲区;类似于KeyValuePairList.每次在循环中,您都将当前按下的按钮与当前时间一起添加到缓冲区中,删除时间早于某个特定时间跨度的所有内容,然后检查缓冲区的内容是否与任何组合移动相匹配.这允许瞬时组合以及顺序组合(例如,A, A, Left+B将起作用).您可能需要查看同时取消由单个按键触发的任何移动的某种方式-如果您识别出left, jump&punch的组合,那么您将想以某种方式阻止它们跳跃和猛击,而是执行飞行的上切动作组合触发器.

I'd probably look at using an input buffer; something like a List of KeyValuePair. Each time round the loop you add the buttons currently being pressed to the buffer along with the current time, remove anything where the time is older than a certain timespan, and then check whether the contents of the buffer matches any combo moves. This allows for instantaneous combos as well as sequential ones (for example, A, A, Left+B would work). You might need to look at some way of cancelling any moves triggered by the single keypresses in the meantime - if you recognise a combo of left, jump&punch then you'll want to somehow stop them from jumping and punching and instead do the flying uppercut that the combo triggers.

对于轻/重按键,我认为这不可能以您所想的方式实现-电脑键盘是非常简单的设备;这些按钮是简单的开/关单位,没有有关压力或速度的信息.您可以查看按键之间的距离-如果使用上述的缓冲方法,则可以使用按键存储的时间来获得两次按键之间的时间.如果该时间长于某个时间间隔,则可以使用较弱或较慢的移动.

As for the light/heavy keypress, I don't think this is possible in the way you're thinking of - computer keyboards are pretty simple devices; the buttons are simple on/off units with no info about pressure or speed. You could look at how far apart the keypresses are - if you use the buffer approach described, then you could use the times stored with the keypresses to get the time between presses. If that time is longer than a certain interval, you use the weaker or slower move.

这篇关于XNA处理按键组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆