SFML community forums
Help => General => Topic started by: krrice on November 13, 2011, 09:53:39 pm
-
I am having a problem with sprite movement.If I run this code the only key that works is left.Nothing happens with the other keys.Another problems is the sprite moves right off the screen when I press left.Is there something simple I can do or do I need to write the code a different way?
float ElapsedTime = Window.GetFrameTime() ;
// Move the sprite
switch(Event.Type)
{
case sf::Event::KeyPressed:
switch(Event.Key.Code)
case sf::Keyboard::Left:
Sprite.Move(-100 * ElapsedTime, 0);
break;
case sf::Keyboard::Right:
Sprite.Move( 100 * ElapsedTime, 0);
break;
case sf::Keyboard::Up:
Sprite.Move(0, -100 * ElapsedTime);
break;
case sf::Keyboard::Down:
Sprite.Move(0, 100 * ElapsedTime);
break;
default:
break;
}
-
Pay attention to the structure of your switch statements.
You have 2 levels of switch (they are nested), so you have to write the correct code.
Try this!
(I highlighted with an arrow the lines I added)
float ElapsedTime = Window.GetFrameTime() ;
// Move the sprite
switch(Event.Type)
{
case sf::Event::KeyPressed:
switch(Event.Key.Code)
-> {
case sf::Keyboard::Left:
Sprite.Move(-100 * ElapsedTime, 0);
break;
case sf::Keyboard::Right:
Sprite.Move( 100 * ElapsedTime, 0);
break;
case sf::Keyboard::Up:
Sprite.Move(0, -100 * ElapsedTime);
break;
case sf::Keyboard::Down:
Sprite.Move(0, 100 * ElapsedTime);
break;
default:
break;
-> }
-> break;
-> default:
-> break;
}
Another hint: 0 and 100 are considered integer numbers from compiler, use 0.f and 100.f instead, 'cause ElapsedTime is a float.
-
Thanks that worked but the movement is choppy and then the spite dissapears is there a way to fix that?
-
Thanks that worked but the movement is choppy is there a way to fix that?
Try (if you're using sfml1.6):
App.UseVerticalSync(true);
where App is your rendering window.
This command limits framerate to vertical refresh of your current video mode.
-
I am using sfml2
-
I am using sfml2
In that case use:
App.EnableVerticalSync( true );
-
Thanks a lot for your help.It works now but I had to devide my GetFrameRate() / 200 to get it right. One more question if you don't mind. It pauses when I change directions is there something I can do about that?
-
Thanks a lot for your help.It works now but I had to devide my GetFrameRate() / 200 to get it right. One more question if you don't mind. It pauses when I change directions is there something I can do about that?
There is another way to handle inputs continuosly (without waiting for a key will repeat if held longer).
In sfml 1.6 you could use, for example:
App.GetInput().IsKeyDown(sf::Key::Right)
I don't know how to do it on SFML 2. :/
-
In SFML2.0 is just easier.
sf::Keyboard::IsKeyPressed(sf::Key::Right);
There are sf::Keyboard and sf::Mouse for this purpose. Get a look to the doc.
-
Thank's I must have to use it in a if statement could not figure out how to use it in a switch.
-
Instead of updating the sprite's position in your event logic, you could toggle a boolean and update the position of your sprite based on the boolean's value. Here's an example:
bool right, left;
float x, y;
Handling input:
switch (event.Type)
{
case sf::Event::KeyPressed:
if (event.Key.Code == sf::Keyboard::Right)
{
right = true;
}
if (event.Key.Code == sf::Keyboard::Left)
{
left = true;
}
break;
case sf::Event::KeyReleased:
if (event.Key.Code == sf::Keyboard::Right)
{
right = false;
}
if (event.Key.Code == sf::Keyboard::Left)
{
left = false;
}
break;
}
Update loop:
sprite.SetPosition(x, y);
if (right == true && left == false)
{
x += offset;
}
if (right == false && left == true)
{
x -= offset;
}