while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))()&&accelerationx > 15;
{
accelerationx = accelerationx + 5;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))()&&accelerationx > 0;
{
accelerationx = accelerationx -5;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))()&&accelerationx > 15;
{
accelerationy = accelerationy + 5;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))()&&accelerationx > 0;
{
accelerationy = accelerationy - 5;
}
speedx = accelerationx + speedx;
speedy = accelerationy + speedy;
sprite.move(sf::Vector2f(0.f+speedx, 0.f+speedy));
}
// Clear screen
window.clear();
// Draw the sprite
window.draw(sprite);
// Update the window
window.display();
}
return EXIT_SUCCESS;
}
返回值:
error: expected primary-expression before ')' token
C++和SFML的新手,如果这是一个愚蠢的问题,我们深表歉意。 Lorem ipsum dolor坐下来,自私自利。 Fusce dui erat,Bactit eget facilisis ac,euismod non metus。
最佳答案
if
之后的表达式(条件)必须在括号()
中,并且不能以分号;
终止。
如果if
块中只有1个命令,则无需将其放在大括号{}
之间:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) && accelerationx > 15)
accelerationx = accelerationx + 5;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) && accelerationx > 0)
accelerationx = accelerationx -5;
此外,最好使用赋值运算符(
+=
,-=
)来增加/减少值: if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) && accelerationx > 15)
accelerationx += 5;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) && accelerationx > 0)
accelerationx -= 5;
关于c++ - SFML:错误: ')' token 之前的预期主表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59976334/