SFML community forums

Help => Graphics => Topic started by: MaeZer on May 27, 2015, 05:56:05 pm

Title: Loop a sprite's movement?
Post by: MaeZer on May 27, 2015, 05:56:05 pm
I'm a bit stuck with this one, I'm trying to make a sprite move back and forth forever, while the game loop is running. At first I tried to use a timer/clock function, but I couldn't get a library/function that I could use. What I have tried now though is having 2 invisible rects, one at each end of the sprite's path. So when the sprite intersects one, it's direction is reversed and so on. However in my code I can't figure out where I've gone wrong. Any thoughts? Sorry if it is a bit hard to understand in the code.


        bool targmright = false; // bool for moving the sprite right
        bool targmleft = true; // bool for moving the sprite left

                        if (targmleft == true) // "targ" is the sprite I mention
                        {
                       
                                targ.move(-1, 0);
                               
                                if (targb.intersects(targlb))   // if the sprite's bounding box intersects the left bounding box
                                {
                                        targmright = true;
                                targmleft = false;
}
                               
                                       
                       
                        }

                        if (targmright == true)
                        {

                                targ.move(1, 0);
                       
                               
                                if (targb.intersects(targrb))
                                {
                                        targmright = true;
                                        targmleft = false;
                                }

                        }
 
Title: Re: Loop a sprite's movement?
Post by: Jesper Juhl on May 27, 2015, 06:05:10 pm
Maybe these links will help:

I have an example on the wiki of a little program that bounces a ball around the screen. Maybe that can provide some inspiration: https://github.com/SFML/SFML/wiki/Source:-Bouncing-ball

If I misunderstood you and what you are really after is animation, then take a look at Thor's Animations module: http://www.bromeon.ch/libraries/thor/v2.0/doc/group___animations.html

I hope that helps :)
Title: Re: Loop a sprite's movement?
Post by: kitteh-warrior on May 27, 2015, 07:20:15 pm
When you are checking if there is an intersection between targb and targrb, you are setting targmright to true, and targmleft to false. I believe you want to do the opposite.
I have not tested this, but I believe that might be the cause of the problem.

Edit: I assumed that the Boolean values were declared prior to a loop, and the movement checking is happening within the loop. However, I am uncertain if that is the case here.
Title: Re: Loop a sprite's movement?
Post by: DarkRoku12 on May 29, 2015, 04:12:54 pm
You can use for better coordinates :

Ex:

// outside the game loop :
int max_left = 0
int max_right = 600 /* (supposing that youw window have 600 width ... you can change the size in the
resize // event */

float move = 1 ;  

// inside the game loop :

floatRect spr_pos = targ.getPosition()

if ( spr_pos.x >= max_right )
{  
    move = -1
}
else if (  spr_pos.x <= max_left )
{
   move = 1
}
targ.move( move , 0 )