Hi hi,
I'm creating a game for my portfolio and at present it's a simple sprite running around a screen (no rotations or anything implemented yet) and I'm finding a minor issue quite annoying.
If a player is holding down the up key to move vertically and then hits the right key to head right, whilst still holding down the up keupon release of the right key the sprite doesn't move upwards as you'd expect.
I can kind of see why it does what it's doing, but I can't think of a solution. Any ideas?
Here's my input function:
[source lang="cpp"]void Game::getInput(){ sf::Event Event; while (MyGameWindow->GetEvent(Event)) { if (Event.Type == sf::Event::Closed) MyGameWindow->Close(); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) MyGameWindow->Close(); if(Event.Type == sf::Event::KeyPressed) currentKey = Event.Key.Code; if(Event.Type == sf::Event::KeyReleased) currentKey = sf::Key::Numpad9; if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::R)) paused = false; }}[/source]
And then my update function:
[source lang="cpp"]void Game::update(){ if(!paused) { ELAPSED_TIME = myClock.GetElapsedTime(); if (ELAPSED_TIME > FRAMERATE) { switch(currentKey) { case sf::Key::Up: P1.MoveUp(10); break; case sf::Key::Down: P1.MoveDown(10); break; case sf::Key::Left: P1.MoveLeft(10); break; case sf::Key::Right: P1.MoveRight(10); break; case sf::Key:[img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]: paused = true; break; } myClock.Reset(); } }}[/source]
Cheers guys!
SFML - Key Presses?
Started by Dezachu, Nov 28 2012 03:32 PM
3 replies to this topic
Sponsor:
#2 Members - Reputation: 520
Posted 28 November 2012 - 03:49 PM
Try to use booleans like
[source lang="cpp"]bool upPressed = false;......if( ...( sf::Key::Up) ){ upPressed = true;}[/source]
and obviously if not sf::Key::Up
upPressed = false;
That will be only the input.
So in the game logic you need to put something like:
[source lang="cpp"]if( upPressed ){ P1.MoveUp( 10 );}[/source]
[source lang="cpp"]bool upPressed = false;......if( ...( sf::Key::Up) ){ upPressed = true;}[/source]
and obviously if not sf::Key::Up
upPressed = false;
That will be only the input.
So in the game logic you need to put something like:
[source lang="cpp"]if( upPressed ){ P1.MoveUp( 10 );}[/source]
#4 Members - Reputation: 1566
Posted 29 November 2012 - 01:20 PM
My suggestion would be to give your character a velocity when certain buttons have been pressed. Something like this (pseudo-code):
Good luck!
#define DEFAULT_SPEED 10
// If a key is pressed, give it an initial velocity
if (Key.IsPressed) {
// handle X movement
if (Key.Type == RIGHT) {
Player.xVelocity += DEFAULT_SPEED;
}
else if (Key.Type == LEFT) {
Player.xVelocity += -DEFAULT_SPEED;
}
// Handle Y movement
if (Key.Type == UP) {
Player.yVelocity += -DEFAULT_SPEED;
}
else if (Key.Type == DOWN) {
Player.yVelocity += DEFAULT_SPEED;
}
}
// When a key is release, subtract that same velocity
if (Key.IsReleased) {
if (Key.Type == RIGHT) {
Player.xVelocity -= DEFAULT_SPEED;
}
else if (Key.Type == LEFT) {
Player.xVelocity -= -DEFAULT_SPEED;
}
if (Key.Type == UP) {
Player.yVelocity -= -DEFAULT_SPEED;
}
else if (Key.Type == DOWN) {
Player.yVelocity -= DEFAULT_SPEED;
}
}
// When moving, just move the player by their veloctities in both directions)
Good luck!
Edited by BeerNutts, 29 November 2012 - 01:21 PM.
My Gamedev Journal: 2D Game Making, the Easy Way
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)






