Jump to content
  • Advertisement
Sign in to follow this  
Dezachu

SFML - Key Presses?

This topic is 2122 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
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! Edited by BeerNutts

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!