gpt4 book ai didi

c++ - SFML 按钮触发两次

转载 作者:太空宇宙 更新时间:2023-11-04 12:53:10 24 4
gpt4 key购买 nike

我正在使用 SFML 编写游戏,但我的按钮出现了小问题。这是我用来检查按钮是否被单击的函数:

bool InputManager::isSpriteClicked(sf::Sprite object, sf::Mouse::Button button, sf::RenderWindow &window) {

if (sf::Mouse::isButtonPressed(button)) {
sf::IntRect rect(object.getPosition().x, object.getPosition().y,
object.getGlobalBounds().width, object.getGlobalBounds().height);
if (rect.contains(sf::Mouse::getPosition(window))) {
return true;
}
}
return false;
}

它几乎可以正常工作,但有时一旦我按下这个按钮, Action 就会被触发两次,就像我双击它一样,即使我还没有释放它。我试图让 sf::Event::MouseButtonReleased 参与进来,但它实际上也无济于事。我想要实现的当然是每按下/释放 1 个按钮/无论什么,只需 1 个 Action 。

如果需要,这里是我的GameLoop示例

void GameState::handleUserInput() {
sf::Event evnt;
while (this->m_data->window.pollEvent(evnt)) {

if (this->m_data->input.isSpriteClicked(this->m_rollButton, sf::Mouse::Left, this->m_data->window)) {
m_gameEngine.startTurn(m_gameStatusBox);
}
}

void GameState::update(sf::Time dt) {
m_gameEngine.getActivePlayer().move(dt);
}

void GameState::draw() {
this->m_data->window.display();
}

最佳答案

简答:

    if (event.type == sf::Event::MouseButtonPressed)

Long story顺便说一句,它是谷歌中的第二个链接....

关于c++ - SFML 按钮触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47947584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com