SFML community forums

Help => General => Topic started by: krrice on November 13, 2011, 09:53:39 pm

Title: Sprite movement
Post 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?




Code: [Select]
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;


        }
Title: Re: Sprite movement
Post by: Naufr4g0 on November 13, 2011, 10:16:16 pm
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)

Code: [Select]

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.
Title: Sprite movement
Post by: krrice on November 13, 2011, 10:25:01 pm
Thanks that worked but the movement is choppy and then the spite dissapears is there a way to fix that?
Title: Sprite movement
Post by: Naufr4g0 on November 13, 2011, 10:29:00 pm
Quote from: "krrice"
Thanks that worked but the movement is choppy is there a way to fix that?


Try (if you're using sfml1.6):
Code: [Select]

App.UseVerticalSync(true);

where App is your rendering window.
This command limits framerate to vertical refresh of your current video mode.
Title: Sprite movement
Post by: krrice on November 13, 2011, 10:32:16 pm
I am using sfml2
Title: Sprite movement
Post by: Naufr4g0 on November 13, 2011, 10:42:44 pm
Quote from: "krrice"
I am using sfml2


In that case use:
Code: [Select]

App.EnableVerticalSync( true );
Title: Sprite movement
Post by: krrice on November 13, 2011, 10:56:16 pm
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?
Title: Sprite movement
Post by: Naufr4g0 on November 13, 2011, 11:08:20 pm
Quote from: "krrice"
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:
Code: [Select]

App.GetInput().IsKeyDown(sf::Key::Right)

I don't know how to do it on SFML 2. :/
Title: Sprite movement
Post by: julen26 on November 13, 2011, 11:44:38 pm
In SFML2.0 is just easier.
Code: [Select]

sf::Keyboard::IsKeyPressed(sf::Key::Right);

There are sf::Keyboard and sf::Mouse for this purpose. Get a look to the doc.
Title: Sprite movement
Post by: krrice on November 14, 2011, 12:01:50 am
Thank's I must have to use it in a if statement could not figure out how to use it in a switch.
Title: Sprite movement
Post by: fatum on November 14, 2011, 09:32:44 am
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:

Code: [Select]

bool right, left;
float x, y;


Handling input:
Code: [Select]

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:
Code: [Select]

sprite.SetPosition(x, y);
if (right == true && left == false)
{
x += offset;
}

if (right == false && left == true)
{
x -= offset;
}