I'm struggling with something painfully basic here, so please bear with me.
I'm going to try some simple platforming mechanics, and of course the first order of business is walking back and forth on the screen. There's no collision detection or velocity or anything yet, I've just got keys changing X position. Anyway, that's all well and good, but my placeholder Megaman sprite should be facing the direction he is moving.
What I've done right now is made an "isXFlipped" boolean, which is initialized as false. Then, with event polling, I check if the "right" key is pressed and if "isXFlipped" is false, and if it is I make "isXFlipped" true (as left is the sprite's default direction), and call sprite.Scale(-1.f, 1.f) (to X flip with SFML 2.0). Then, if the "left" key is pressed and "isXFlipped" is true, I make "isXFlipped" false and call sprite.Scale(-1.f, 1.f).
bool Engine::Init()
{
...
isXFlipped = false;
...
}
void Engine::processInput()
{
...
while(mainWindow.PollEvent(Event))
{
...
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Right) && isXFlipped == false)
{
isXFlipped = true;
playerSprite.Scale(-1.f, 1.f);
}
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Left) && isXFlipped == true)
{
isXFlipped = false;
playerSprite.Scale(-1.f, 1.f);
}
}
float deltaTime = frameClock.GetElapsedTime().AsSeconds();
if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Left))
{
playerSprite.Move(-1150 * deltaTime, 0);
}
if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Right))
{
playerSprite.Move(1150 * deltaTime, 0);
}
...
This works alright, Megman flips left (if he is not already) when I press left and right (if he is not already) when I press right. He also moves those directions. The problem comes when I try pressing BOTH keys.
For example, when I am pressing right, the sprite is facing right and is walking right, but if, while I am still pressing right, I press left, the sprite stops moving altogether and faces left. This is a bit of a problem, as I would like for the new direction to take precedence over the old one (so pressing right then left would make you move left, and letting go of left if you were still holding right makes you go right). But, even worse, if I let go of left, but I am still holding right, the sprite moves right but continues to face left. This is probably horribly confusing in words, so here is a diagram:
http://i.imgur.com/8V5jB8Q.png(I'm honestly not sure if that makes more sense or not)
Anyway, thanks for reading my stupid thread!