Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


SFML - Key Presses?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Dezachu   Members   -  Reputation: 386

Like
0Likes
Like

Posted 28 November 2012 - 03:32 PM

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!

Studying BSc Computer Games Programming @ De Montfort University, Leicester.

Completed a placement as a Junior Programmer at Exient Ltd in Oxford/Valletta, Malta


Sponsor:

#2 riverreal   Members   -  Reputation: 616

Like
0Likes
Like

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]

#3 Dezachu   Members   -  Reputation: 386

Like
0Likes
Like

Posted 28 November 2012 - 05:53 PM

Thanks, I'll try implementing it later :)

Studying BSc Computer Games Programming @ De Montfort University, Leicester.

Completed a placement as a Junior Programmer at Exient Ltd in Oxford/Valletta, Malta


#4 BeerNutts   Crossbones+   -  Reputation: 2980

Like
0Likes
Like

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):

#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 topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS