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;
}
}