如何检测按键是被按下一次还是被按下? [英] How to detect if key is pressed once or is being held down?

查看:250
本文介绍了如何检测按键是被按下一次还是被按下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作  direct2d中的平台游戏。我遇到跳跃问题。我做了一个逻辑,如果"w",按下按键给角色一个负速度,所以它会向上移动一段距离然后向下移动。我的问题是如果按下"w"键
,我的角色就​​会飞起来。我希望跳转只发生一次。我试过这个: -

I am making  a platform game in direct2d. I am having a problem with jump. I made a logic that if the "w" key is pressed a negative speed is given to the character, so it goes up some distance and comes down. My problem is that if the "w"key is held down my character goes flying up. I want the jump to happen only once. I tried this:-

if((GetAsyncKeyState('W')&(0x1)==(0x1))
....do jump


但是在这种情况下我的角色会跳跃,稍微下降,如果w被举起,他们就会飞起来。我发现另一种方法是在跳跃之前检查我的角色是否在地面上工作。但是我也想检测"D"和"S"
是否被按下以便我可以运行步行动画然后在释放按键时返回站立。有没有办法这样做?请帮助我

but in this case my character jumps, come down a bit and they  goes flying up if w is held. I found another way that I check if my character is on ground before jumping which is working. But I also want to detect whether "D" and "S" are held down so I can run the walking animation and then return to standing when key is released. is there any way do so? pls help me

推荐答案

您好,

感谢您在此处发帖。

>>我正在制作一个direct2d的平台游戏。我遇到跳跃问题。我做了一个逻辑,如果按下"w"键,则会给角色带来负速度,所以它会去我的问题是
,如果按下"w"键,我的角色会飞起来......

目前为止我所知,你可以试试这个:

As far as I know, you could try this:

if (GetAsyncKeyState('W')&(0x8000)) 
{
      //   press W
      if (GetAsyncKeyState('D')&(0x8000)) 
      {
	    //press D
      }
      
      if (GetAsyncKeyState('S')&(0x8000)) 
      {
	    //press S
      }
}

祝福,

Jack Zhang

Jack Zhang


这篇关于如何检测按键是被按下一次还是被按下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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