Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] Yet another isolation of single Key events  (Read 4241 times)

0 Members and 1 Guest are viewing this topic.

brainydexter

  • Newbie
  • *
  • Posts: 5
    • Yahoo Instant Messenger - fly2priyank@yahoo.com
    • View Profile
    • http://cghost.origo.ethz.ch/blog
[Solved] Yet another isolation of single Key events
« on: June 13, 2008, 03:19:45 pm »
Hi..

Im trying to work out a game where the character movement is governed by arrow keys. I have used both :

Code: [Select]
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Right))
        xspeed = 1.0f;


AND

Code: [Select]
if(Input.IsKeyDown(sf::Key::Right)) xspeed = 1.0f;

I do understand that the snippet 1 is present for single key presses and the latter one for situations with continous key presses. Now in my case, the latter one should be the solution since I want my character to keep moving till the time I release the arrow key.

To my misfortune, even when I press the right key (in this case) just once..my character shows a displacement which corresponds to more than 1 key hit event.. it shows an equivalent displacement of 2-3 key hits..

Any ideas.. how I can rectify this and why is the system behaving as it is ?

Thanks
Dexter[/code]
Daamn..I broke it again~

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Solved] Yet another isolation of single Key events
« Reply #1 on: June 13, 2008, 03:30:09 pm »
Can you show the code which moves the sprite using the xspeed variable ?
Laurent Gomila - SFML developer

brainydexter

  • Newbie
  • *
  • Posts: 5
    • Yahoo Instant Messenger - fly2priyank@yahoo.com
    • View Profile
    • http://cghost.origo.ethz.ch/blog
[Solved] Yet another isolation of single Key events
« Reply #2 on: June 13, 2008, 03:50:42 pm »
This is essentially the code.. This is within the player class.. I dint put in other things here to keep things simple.

Code: [Select]
   
x += int (xspeed * TILEW);


After this code.. I put in this to restore the effect of speed, so that on the next loop count..if there is no key press..there is no hangover from the above xspeed.

Code: [Select]
xspeed = 0.0f;
Daamn..I broke it again~

brainydexter

  • Newbie
  • *
  • Posts: 5
    • Yahoo Instant Messenger - fly2priyank@yahoo.com
    • View Profile
    • http://cghost.origo.ethz.ch/blog
[Solved] Yet another isolation of single Key events
« Reply #3 on: June 14, 2008, 11:47:16 pm »
I was able to solve the problem.. I moved in a few statements here and there, moreso.. for handling events, I had a problem with polling. Once, I moved in the key handling event statements within the polling loop, things went smooth.

Still thanks for the help  :)

Dexter
Daamn..I broke it again~

 

anything