c++ - SFML:错误: ')' token 之前的预期主表达式

标签 c++ sfml

  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/

相关文章:

c++ - 自动生成枚举查找表

c++ - recv() 字符数组大小

c++ - 查找未排序的 vector<int>

c# - 我可以使用 SFML 2.1 2d 绘图 API 实现裁剪吗?

C++ Box2D - 单独放置时不受重力影响的动态物体

c++ - 在 SFML 事件中发送鼠标位置

c++ - ReadProcessMemory 比 SharedMemory 上的 memcpy 更快

c++ - 在父窗口所在屏幕上显示子窗口

c++ - 在运行时将主类对象的类型更改为子类

c++ - 导致奇怪行为的共享指针