SFML community forums

Help => Window => Topic started by: brainydexter on June 13, 2008, 03:19:45 pm

Title: [Solved] Yet another isolation of single Key events
Post by: brainydexter 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]
Title: [Solved] Yet another isolation of single Key events
Post by: Laurent on June 13, 2008, 03:30:09 pm
Can you show the code which moves the sprite using the xspeed variable ?
Title: [Solved] Yet another isolation of single Key events
Post by: brainydexter 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;
Title: [Solved] Yet another isolation of single Key events
Post by: brainydexter 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