SFML community forums
Help => General => Topic started by: danger on March 20, 2014, 11:25:06 pm
-
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.
-
Do you have "handePlayerInput()" declared anywhere? :)
Declaring != defining
-
Please use the [code=cpp]// Your Code[/code] when posting code.
The problem is, that handlePlayerInput was not declared in the class itself (there's not void handlePlayerInput(sf::Keyboard::Key, key bool isPressed); directly within the class Game).
If you're unable to solve such a basic problem, it might be best to take a step back and learn some more C++ basics. The SFML Game Development book is not a "How to programming in C++" book, instead it requires quite a bit of C++ knowledge to begin with. If you get a C++ book, make sure to pick a good one (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).
-
Please use the [code=cpp]// Your Code[/code] when posting code.
The problem is, that handlePlayerInput was not declared in the class itself (there's not void handlePlayerInput(sf::Keyboard::Key, key bool isPressed); directly within the class Game).
If you're unable to solve such a basic problem, it might be best to take a step back and learn some more C++ basics. The SFML Game Development book is not a "How to programming in C++" book, instead it requires quite a bit of C++ knowledge to begin with. If you get a C++ book, make sure to pick a good one (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).
Thanks for the reply, first off I am learning the C++ language alongside with this, I do have a basic knowledge of language so far I am attempting to learn in tandem. As I said in the list at the bottom I did attempt to declare the function in class game and it still is giving me the same error.
-
As I said in the list at the bottom I did attempt to declare the function in class game and it still is giving me the same error.
You should always show the code of what you actually have. ;)
Also there are some spaces right there, not sure if that's intended or just some c&p issue:
sf::Color:: Cyan
void Game:: handlePlayerInput(sf::Keyboard::Key, key bool isPressed)
void Game ::processEvents()
So again, please provide the cleaned up code (in code tags) that generate the error and provide the error message. :)
-
You should always show the code of what you actually have. ;)
Also there are some spaces right there, not sure if that's intended or just some c&p issue:
sf::Color:: Cyan
void Game:: handlePlayerInput(sf::Keyboard::Key, key bool isPressed)
void Game ::processEvents()
So again, please provide the cleaned up code (in code tags) that generate the error and provide the error message. :)
First off thank you for taking the time again to reply. I will do a better job in the future of posting the code properly so it isn't so confusing for others. I did figure out the issue. I was incorrectly initializing the function in the game class. Like I said I am learning C++ along side of SFmL. I only have about a 3 month headstart in C++ over SFML. I thought this error I was making was more of an oversight rather than a lack of knowledge (which it looks like it was,) Thanks again for the advice though.