EDIT: Fixed it! Nevermind!
The move code is in the original post. Here it is again:
enum Direction { left, right, up, down };
class CMove
{
public:
CMove(sf::Shape& obj)
: object(&obj)
{ }
void WalkLeft() // walk left
{
dir = left;
Walk(dir);
}
void WalkRight() // walk right
{
dir = right;
Walk(dir);
}
void WalkUp() // walk up
{
dir = up;
Walk(dir);
}
void WalkDown() // walk down
{
dir = down;
Walk(dir);
}
private:
Direction dir; // the direction to move in
sf::Shape* object; // pointer to the object to move
void Walk(Direction dir) const; // move the object
};
// move the player in the direction specified in dir
void CMove::Walk(Direction dir) const
{
switch(dir)
{
case left:
object->Move(-5, 0);
case right:
object->Move(5, 0);
case up:
object->Move(0, -5);
case down:
object->Move(0, 5);
}
return;
}
As for it being a poltergeist class I moved the instantiation of the Move object outside of the main loop so it's only instantiated once. The main function looks like this now:
#include<SFML/Graphics.hpp>
#include<iostream>
#include "Move.h"
void handleMovement(const sf::Input& Input, CMove* Move) // handle movement of player
{
bool leftKey = Input.IsKeyDown(sf::Key::Left);
bool rightKey = Input.IsKeyDown( sf::Key::Right);
bool upKey = Input.IsKeyDown(sf::Key::Up);
bool downKey = Input.IsKeyDown(sf::Key::Down);
if(leftKey)
Move->WalkLeft();
if(rightKey)
Move->WalkRight();
if(upKey)
Move->WalkUp();
if(downKey)
Move->WalkDown();
}
int main(void)
{
sf::RenderWindow App(sf::VideoMode(800, 640, 32), "SFML Window");
App.SetFramerateLimit(60);
sf::Shape rect = sf::Shape::Rectangle(300, 300, 350, 350, sf::Color(200, 200, 200));
CMove Move(rect);
CMove* pMove = &Move;
while(App.IsOpened())
{
sf::Event Event;
const sf::Input& Input = App.GetInput();
while(App.GetEvent(Event))
{
// handle movement
handleMovement(Input, pMove);
if(Event.Type == sf::Event::Closed)
App.Close();
}
App.Clear();
App.Draw(rect);
App.Display();
}
return EXIT_SUCCESS;
}