I think the subject is the problem at least. This is my code, it is from the book SFML Game Development. I think I am overlooking some simply aspect like a misplace function, but I have tried several things to no avail.
edit:Sorry forgot to mention the compiler error I am getting is "handlePlayerInput( etc...) has not been declared in this scope
Edit 2: I'll leave everything under here as it originally was. The solution that I figured out + based on the opinions of the others below was I was simply declaring the function incorrectly, thanks for those that helped. And to people who are new to SFML what I learned from this is sometimes it is a simple syntax error so be diligent in re-reading your code.
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
class Game
{
public:
Game();
void run();
private:
void processEvents();
void update();
void render();
private:
sf::RenderWindow mWindow;
sf::CircleShape mPlayer;
};
int main ()
{
Game game;
game.run();
}
Game::Game()
: mWindow(sf::VideoMode (640, 480), "SFML APPLICATION")
,mPlayer()
{
mPlayer.setRadius(40.f);
mPlayer.setPosition(100.f, 100.f);
mPlayer.setFillColor(sf::Color:: Cyan);
}
void Game::run()
{
while (mWindow.isOpen())
{
processEvents();
update();
render();
}
}
void Game ::processEvents()
{
sf::Event event;
while (mWindow.pollEvent (event))
{
switch (event.type)
{
case sf::Event::KeyPressed:
handlePlayerInput (event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
break:
case sf::Event::Closed:
mWindow.close();
}
}
}
void Game::update()
{
sfVector2f movement (0.f, 0.);
if (mIsMovingUp)
movement.y -= 1.f;
if (mIsMovingdown)
movement.y += 1.f;
if (mIsMovingLeft)
movement.x -= 1.f;
if (mIsMovingRight)
movement.x += 1.f;
}
void Game::render()
{
mWindow.clear();
mWindow.draw(mPlayer);
mWindow.display();
}
void Game:: handlePlayerInput(sf::Keyboard::Key, key bool isPressed)
{
if (key sf::Keyboard::w)
mIsMovingUp = isPressed;
else if (key sf::Keyboard::S)
mIsMovingDown = isPressed;
else if (key sf::Keyboard::A)
mIsMovingLeft = isPressed;
else if (key sf::Keyboard:: D)
mIsMovingRight = isPressed;
}
Things I have tried unsuccessfully
- I tried Declaring the handlePlayerInput() before the processEvent() in a void statement
- I tried adding the function to the list in Class Game private.
- I tried to add the handlePlayerIput() into the processEvents()
Thank you for anyone that takes time to help me with this, I feel like it is something simple that I am overlooking. I will continue to work on it on my own, and will edit this if I figure out a solution.