I've been working on an input manager for a day or two. For some reason it isn't working like it should be. It works fine when using the arrow keys, but when using the wasd keys it acts like it isn't getting any input.
InputManager.hpp
#ifndef INPUTMANAGER_H
#define INPUTMANAGER_H
#include <SFML/Graphics.hpp>
class InputManager
{
private:
sf::Event inputEvent;
public:
InputManager(sf::Event);
bool IsKeyPressed(sf::Keyboard::Key);
};
#endif
InputManager.cpp
#include "InputManager.hpp"
#include <SFML/Graphics.hpp>
#include <iostream>
InputManager::InputManager(sf::Event event)
{
inputEvent = event;
}
bool InputManager::IsKeyPressed(sf::Keyboard::Key key)
{
if(inputEvent.type == sf::Event::KeyPressed)
{
if(inputEvent.key.code == key)
{
return true;
}
}
return false;
}
Main loop code
while(window.isOpen())
{
sf::Event Event;
InputManager *input = new InputManager(Event);
while(window.pollEvent(Event))
{
//if(Event.Type == sf::Event::Closed || Event.Key.Code == sf::Key::Escape)
if(Event.type == sf::Event::Closed || input->IsKeyPressed(sf::Keyboard::Escape))
window.close();
}
//if(Window.GetInput().IsKeyDown(sf::Key::Right))
if(input->IsKeyPressed(sf::Keyboard::D))
playerVelocity.x = moveSpeed;
else if(input->IsKeyPressed(sf::Keyboard::A))
playerVelocity.x = -moveSpeed;
else
playerVelocity.x = 0;
if(input->IsKeyPressed(sf::Keyboard::W) && jump)
{
playerVelocity.y = -jumpSpeed;
jump = false;
}
if(input->IsKeyPressed(sf::Keyboard::Space))
{
rect.setFillColor(sf::Color::Red);
}
if(!jump)
playerVelocity.y += gravity;
else
playerVelocity.y = 0;
playerPosition += playerVelocity;
jump = playerPosition.y + 10 >= groundHeight;
if(jump)
playerPosition.y = groundHeight - 10;
rect.setPosition(playerPosition);
window.clear();
level->DrawMap(window);
window.draw(rect);
window.display();
delete input;
}
I'm using Xubuntu 12.04 if that matters and everything is being linked dynamically.
Any help on this will be appreciated. Thanks!