SFML 2.0中的关键重复 [英] Key Repetition in SFML 2.0

查看:264
本文介绍了SFML 2.0中的关键重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某个键刚刚被按下(未持有)在SFML中,或者是模仿这种效果的方式,有没有办法接收?



event here,

  while(window.pollEvent(event)); 

然后在循环中,超出范围,但是同一个事件, p>

  if(event.type == sf :: Event :: KeyPressed)
{
if key.code == sf :: Keyboard :: Up)
{
decision--;
if(decision< 0)
decision = CHARACTER_MAX - 1;
}
}

code>按住向上箭头时连续递减。使用 sf :: event s,我可以在第一次按下时读取,而不是在按下时读取?



此外,如果它不可能或你不熟悉sfml,什么是最好的方式来模仿这样与很少或没有全球化变量? (例如 is_already_held ),例如 http://gamedev.stackexchange.com/questions/30826/action-



我意识到这可能是唯一的方法(在更广泛的范围内声明一个变量),但是我

解决方案

您可以从全局变量中清除这些变量。可以使用 sf :: Window :: setKeyRepeatEnabled 禁用重复键:

  window.setKeyRepeatEnabled(false); 

创建窗口后调用一次(足够)。



本文档是此处。 p>




如果由于任何原因不想使用此函数,则可以有一个布尔表来存储每个键的状态。这里是一个伪代码,显示如何保持表更新并集成您的'决策'代码。

  / Init表的状态。 true表示向下,false表示向上。 
std :: array< bool,sf :: Keyboard :: KeyCount> keyState;
keyState.fill(true);

//:
//:

//在事件循环中:
if(event.type == sf :: Event :: KeyPressed)
{
//如果键是UP,并且以前没有按下:
if(event.key.code == sf :: Keyboard :: Up&&!keyState [event.key.code])
{
decision--;
if(decision< 0)
decision = CHARACTER_MAX - 1;
}

//下面是代码的其余部分和if体的结尾:

//更新当前键的状态:
keyState [event.key.code] = true;
}
else if(event.type == sf :: Event :: KeyReleased)
{
//更新当前密钥的状态:
keyState [event。 key.code] = false;
}






第三种方法是请使用第三方库,例如 Thor 。它仍在开发中,因此您可以期望作者的一些更改。在这里,您可以在 >操作 - 这正是您需要的。


Is there a way to receive if a key has just been pressed (not held) in SFML, or a way to mimic such an effect?

I poll my event here,

while (window.pollEvent(event));

And then later in the loop, out of scope but with the same event, I read:

if (event.type == sf::Event::KeyPressed)
{
    if (event.key.code == sf::Keyboard::Up)
    {
        decision--;
        if (decision < 0)
        decision = CHARACTER_MAX - 1;
    }
}

This makes decision decrement continuously when the up arrow is held down. Using sf::events, can I read the first time it is pressed and not when it is held down?

Also, if its not possible or you're not familiar with sfml, what would be the best way to mimic such with little or no globalized variables? (such as is_already_held), e.g. http://gamedev.stackexchange.com/questions/30826/action-button-only-true-once-per-press

I realize that such may be the only way (declaring a variable in a wider scope) but I would prefer not to, because I exit the scope continuously and try to steer clear from global variables as often as possible.

解决方案

You can disable key repetition with sf::Window::setKeyRepeatEnabled:

window.setKeyRepeatEnabled(false);

Call it once (that's enough) after creating your window.

The documentation is here.


If, for any reason, you don't want to use this function, you can have a table of boolean to store the state of each key. Here is a pseudo code that shows how to keep the table up-to-date and integrates your 'decision' code.

// Init the table of state. true means down, false means up.
std::array<bool, sf::Keyboard::KeyCount> keyState;
keyState.fill(true);

// :
// :

// In the event loop:
if (event.type == sf::Event::KeyPressed)
{
    // If the key is UP and was not previously pressed:
    if (event.key.code == sf::Keyboard::Up && !keyState[event.key.code])
    {
        decision--;
        if (decision < 0)
            decision = CHARACTER_MAX - 1;
    }

    // Here comes the rest of your code and at the end of the if-body:

    // Update state of current key:
    keyState[event.key.code] = true;
}
else if (event.type == sf::Event::KeyReleased)
{
    // Update state of current key:
    keyState[event.key.code] = false;
}


A third alternative is to use a third-party library such as Thor. It's still under development so you can expect some changes from the author. And here you'll find a tutorial on Action – that is, exactly what you need.

这篇关于SFML 2.0中的关键重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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