Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

SFML - Key Presses?

Started by Dezachu Nov 28, 2012 at 9:32 PM 2 replies 1.9k views
Original Post
Dezachu
Dezachu
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:tongue.png:
paused = true;
break;
}

myClock.Reset();
}
}
}[/source]



Cheers guys!
BSc Computer Games Programming (De Montfort University) Graduate Worked on Angry Birds Go! at Exient Ltd Co-founder of Stormburst Studios
riverreal
riverreal
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]
Dezachu
Dezachu
Thanks, I'll try implementing it later
BSc Computer Games Programming (De Montfort University) Graduate Worked on Angry Birds Go! at Exient Ltd Co-founder of Stormburst Studios
BeerNutts
BeerNutts
My suggestion would be to give your character a velocity when certain buttons have been pressed. Something like this (pseudo-code):


#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!
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)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.