如何让我的“游戏”获取2个键输入,例如,如果用户点击w并且d向右移动。
顺便说一下,变量eventCheck
是Event
对象
这是我当前的代码(显然不是完整的代码只是事件代码):
while (window.isOpen()) {
Event eventCheck;
while (window.pollEvent(eventCheck)) {
switch (eventCheck.type) {
case Event::Closed:
window.close();
break;
case Event::KeyPressed:
switch (eventCheck.key.code) {
case Keyboard::W:
if (Keyboard::isKeyPressed(Keyboard::A)) {
const Vector2f spritePos = sprite.getPosition();
sprite.setPosition(spritePos.x, spritePos.y - 5);}
break;
case Keyboard::A:
if (Keyboard::isKeyPressed(Keyboard::A)) {
const Vector2f spritePos = sprite.getPosition();
sprite.setPosition(spritePos.x - 5, spritePos.y);}
break;
case Keyboard::S:
if (Keyboard::isKeyPressed(Keyboard::S)) {
const Vector2f spritePos = sprite.getPosition();
sprite.setPosition(spritePos.x, spritePos.y + 5);}
break;
case Keyboard::D:
if (Keyboard::isKeyPressed(Keyboard::D)) {
const Vector2f spritePos = sprite.getPosition();
sprite.setPosition(spritePos.x + 5, spritePos.y);}
break;
}
}
break;
}
window.clear(Color(0,0,0,255));
window.draw(sprite);
window.display();
}
return 0;
答案 0 :(得分:0)
1。)SFML通过sf::Event
进行事件轮询,并通过sf::Keyboard
提供实时密钥状态访问。如果您只需要处理一次按键,最好的方法是通过window.pollEvent
进行轮询,否则通常总是更好地获取密钥的状态并根据状态做出反应。在这种情况下,您将事件轮询与键盘状态混合在一起。选择其中一个。
2.。)不使用sf ::从代码中删除了很多清晰度
现在进入代码!
我不会轮询事件中的任何用户输入。我会得到钥匙的状态:
// Once per game loop
void ProcessInput()
{
int keyCount = 0;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
keyCount++;
//Move Character Up, The more keys are pressed, the more i would mess around with speed/velocity here
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
keyCount++;
//Move Character Left, The more keys are pressed, the more i would mess around with speed/velocity here
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
keyCount++;
//Move Character Down, The more keys are pressed, the more i would mess around with speed/velocity here
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
keyCount++;
//Move Character Right, The more keys are pressed, the more i would mess around with speed/velocity here
}
}
根据用户按下的按键数量,您可能希望减少角色的移动速度,因为它们可以比垂直/水平方向更倾斜地移动,但是如您所知,速度和速度会混乱。
答案 1 :(得分:0)
我最终做了
bool moveL = false;
bool moveU = false;
bool moveD = false;
bool moveR = false;
while (window.isOpen()) {
Event eventCheck;
while (window.pollEvent(eventCheck)) {
switch (eventCheck.type) {
case Event::Closed:
window.close();
break;
case Event::KeyReleased:
switch (eventCheck.key.code) {
case Keyboard::A:
moveL = false;
break;
case Keyboard::W:
moveU = false;
break;
case Keyboard::S:
moveD = false;
break;
case Keyboard::D:
moveR = false;
break;
}
break;
case Event::KeyPressed:
switch (eventCheck.key.code) {
case Keyboard::A:
moveL = true;
break;
case Keyboard::W:
moveU = true;
break;
case Keyboard::S:
moveD = true;
break;
case Keyboard::D:
moveR = true;
break;
case Keyboard::Space:
jump(sprite, window);
break;
}
const Vector2f spritePos = sprite.getPosition();
if (moveD && moveR) {
sprite.setPosition(spritePos.x + 5, spritePos.y + 5);
}
else if (moveL && moveU) {
sprite.setPosition(spritePos.x - 5, spritePos.y - 5);
}
else if (moveU && moveD) {
sprite.setPosition(spritePos.x, spritePos.y);
}
else if (moveU && moveR) {
sprite.setPosition(spritePos.x + 5, spritePos.y - 5);
}
else if (moveD && moveL) {
sprite.setPosition(spritePos.x - 5, spritePos.y + 5);
}
else if (moveL) {
sprite.setPosition(spritePos.x - 5, spritePos.y);
}
else if (moveR) {
sprite.setPosition(spritePos.x + 5, spritePos.y);
}
else if (moveU) {
sprite.setPosition(spritePos.x, spritePos.y - 5);
}
else if (moveD) {
sprite.setPosition(spritePos.x, spritePos.y + 5);
}
break;
}
}
window.clear(Color(0,0,0,255));
window.draw(sprite);
window.display();
}
return 0;
}