SFML: ошибка: ожидаемое первичное выражение до маркера ')' - PullRequest
1 голос
/ 30 января 2020
  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;
}

Возвращает:

ошибка: ожидаемое первичное выражение до ')' токена

Новичок в C ++ и SFML, поэтому извиняюсь, если это глупый вопрос , Lorem Ipsum Dolor Sit Amet, Concetetur Adipiscing Elit. Fusce dui erat, blandit eget facilisis a c, euismod non metus.

1 Ответ

1 голос
/ 30 января 2020

Выражение (условие) после if должно быть в скобках () и НЕ заканчиваться точкой с запятой ;.

Если у вас есть только 1 команда в блоке if, вам не нужно поместите его в фигурные скобки {}:

   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;
...