I'm making a base class for re-use in future projects. Basically a template that i could copy and paste to cut the initial time it takes to start a new project in SFML.
It's all working so far, and i can display my Player object's sprite to the screen. I added a function to my player class so that i can move the sprite around the screen by calling it from my base class, but when i do, it doesn't move. It draws from the base class but won't move when i call player->PlayerControl() from the base class.
Here's my code so you can see what i mean.
Main.cpp
#include <SFML/Graphics.hpp>
#include "Engine.h"
int main()
{
Engine * engine = new Engine();
engine->Go();
return EXIT_SUCCESS;
}
Engine.cpp
void Engine::Go()
{
if(!Init())
{
//Error here
}
}
bool Engine::Init()
{
window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "RPG game");
window->setFramerateLimit(30);
if(!window)
return false;
LoadImages();
MainLoop();
return true;
}
void Engine::LoadImages()
{
sf::Texture playerTexture;
playerTexture.loadFromFile("Link_front.png");
imageManager.AddImage(playerTexture);
player = new Player(imageManager.GetImage(0));
}
void Engine::MainLoop()
{
while(window->isOpen())
{
ProcessInput();
Update();
RenderFrame();
}
}
ProcessInput();
void Engine::ProcessInput()
{
sf::Event Event;
if(window->pollEvent(Event))
{
if(Event.type == sf::Event::Closed)
window->close();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window->close();
}
player->PlayerControl();
}
When i call player->PlayerControl(), nothing happens and the sprite doesn't move at all. Here is the Player.cpp move function:
void Player::PlayerControl()
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
playerSprite.move(0, -2.f);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
playerSprite.move(0, 2.f);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
playerSprite.move(-2.f, 0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
playerSprite.move(2.f, 0);
}
Sorry if this is hard to understand, it's not documented at all, but it should be simple enough to see what's going on as i have placed the functions in the order they are called so top from bottom is the way my game runs.
Why won't my sprite move when the function is called from ProcessInput() ?