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

Author Topic: Loop a sprite's movement?  (Read 1772 times)

0 Members and 1 Guest are viewing this topic.

MaeZer

  • Newbie
  • *
  • Posts: 9
    • View Profile
Loop a sprite's movement?
« 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;
                                }

                        }
 
« Last Edit: May 27, 2015, 05:57:45 pm by MaeZer »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Loop a sprite's movement?
« Reply #1 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 :)

kitteh-warrior

  • Guest
Re: Loop a sprite's movement?
« Reply #2 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.
« Last Edit: May 27, 2015, 08:07:17 pm by kitteh-warrior »

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Loop a sprite's movement?
« Reply #3 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 )
 
I would like a spanish/latin community...
Problems building for Android? Look here