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

Author Topic: Sprite movement  (Read 4560 times)

0 Members and 1 Guest are viewing this topic.

krrice

  • Newbie
  • *
  • Posts: 26
    • View Profile
Sprite movement
« 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;


        }

Naufr4g0

  • Full Member
  • ***
  • Posts: 112
    • View Profile
Re: Sprite movement
« Reply #1 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.

krrice

  • Newbie
  • *
  • Posts: 26
    • View Profile
Sprite movement
« Reply #2 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?

Naufr4g0

  • Full Member
  • ***
  • Posts: 112
    • View Profile
Sprite movement
« Reply #3 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.

krrice

  • Newbie
  • *
  • Posts: 26
    • View Profile
Sprite movement
« Reply #4 on: November 13, 2011, 10:32:16 pm »
I am using sfml2

Naufr4g0

  • Full Member
  • ***
  • Posts: 112
    • View Profile
Sprite movement
« Reply #5 on: November 13, 2011, 10:42:44 pm »
Quote from: "krrice"
I am using sfml2


In that case use:
Code: [Select]

App.EnableVerticalSync( true );

krrice

  • Newbie
  • *
  • Posts: 26
    • View Profile
Sprite movement
« Reply #6 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?

Naufr4g0

  • Full Member
  • ***
  • Posts: 112
    • View Profile
Sprite movement
« Reply #7 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. :/

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
Sprite movement
« Reply #8 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.

krrice

  • Newbie
  • *
  • Posts: 26
    • View Profile
Sprite movement
« Reply #9 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.

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
Sprite movement
« Reply #10 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;
}

 

anything