SFML 2.0 Move to

Oct 9, 2013 at 10:51pm
How do i make it so that a sprite will move to something or a position?
Oct 9, 2013 at 10:58pm
Err... setPosition? =P

 
yoursprite.setPosition( x, y );
Oct 9, 2013 at 11:27pm
no that will teleport it to that position i want it to move there as in move each pixel
Oct 9, 2013 at 11:41pm
Could use move to move it incrementally? Instead of having one call to setPosition, you could use several calls to move where you only move it by some (possibly small) value:
http://sfml-dev.org/documentation/2.0/classsf_1_1Transformable.php#a86b461d6a941ad390c2ad8b6a4a20391

You would also be better off posting to the SFML forums:
http://en.sfml-dev.org/forums/
Oct 9, 2013 at 11:48pm
Yes as Danny Toledo says you have to move it a little bit every frame.

This isn't an SFML problem as much as it's a game logic flow problem. Typical game loops look something like this:

1
2
3
4
5
6
7
8
while(game_is_running)
{
    processEvents();
    updateLogic();
    drawScene();
    waitForNextFrame();  // note:  if you use SFML's frame limiter function to set
       // the FPS, this is done automatically for you.
}


To move an "object" (such as a player graphic, or an enemy, or something)... you typically assign it a position, and a velocity.

Every logic update... you would add the velocity to the position.
Every draw... you would draw the object at its new position.
Oct 10, 2013 at 12:22am
@Disch

What about the move function already provided by SFML? I wouldn't' use setPosition for actual movement lol.

Take in mind what Disch says about the frame problem though. It DOES matter. It's especially noticeable if there are physics in your game. Different computers will have different frames per second. If you don't account for this then on some computers your game will literally run faster, and literally run slower on others. Any game worth it's salt will move objects at the same speed, regardless of FPS.
Oct 10, 2013 at 1:22am
I wouldn't' use setPosition for actual movement lol


I do.

I keep track of position/velocity etc separately anyway for things like physics/collision detection/etc... so I just compute the final position in my code then pass it to SFML directly in the form of a single setPosition call.
Oct 10, 2013 at 1:43am
I do.

I keep track of position/velocity etc separately anyway for things like physics/collision detection/etc... so I just compute the final position in my code then pass it to SFML directly in the form of a single setPosition call.


Eh, in reality the only difference between move() and setPosition() is that move handles offsets for you I would guess. I've never had much trouble with just using move() but I can see where you're coming from.
Oct 10, 2013 at 1:49am
move() would be convenient when you only deal with offsets, but Disch always deals with absolute values so move() is a hindrance. Of course, if you dealt with relative values instead of absolute values, then I'd think move() would be useful. And yes, through some kind of weird luck, I've managed to somehow someway get move() to line up four times in a row. No line breaks, just normal text wrapping.
Last edited on Oct 10, 2013 at 1:51am
Oct 10, 2013 at 1:56am
i know about that way but i was just curious if there was another way
Topic archived. No new replies allowed.