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

Author Topic: Scrolling background loop however produces little gaps  (Read 1862 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Scrolling background loop however produces little gaps
« on: January 27, 2015, 11:38:12 pm »
I have myself a parallaxing background, i'm using SFML to render my background, I have myself 3 sf::Sprites, each sprite has the width of 576, I create myself 3 sprites with x positions 0, 576 and 1152. I then have some if statements that will move the background sprites when they go so far off the screen however... small gaps keep appearing, here is my code for my background manager

void BackgroundManager::Update(int playerXVel)
{
//Update level backgrounds
for (int i = 0; i < 3; i++)
{
    sf::Vector2f postion = levelBackgroundSprites[i].getPosition();
    levelBackgroundSprites[i].setPosition(postion.x += playerXVel, postion.y);

    if (postion.x <= -576)
    {
        levelBackgroundSprites[i].setPosition(1152, postion.y);
    }
    else if (postion.x >= 1152)
    {
        levelBackgroundSprites[i].setPosition(-576, postion.y);
    }
}
}
 

I have attached a screenshot

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Scrolling background loop however produces little gaps
« Reply #1 on: January 27, 2015, 11:41:29 pm »
Try removing one of the equals in the two conditions.

With allowing it to be equal to both sides of the range, it extends the range by 1. e.g. 0 - 10 is 11 values (not 10) and -5 - 10 is 16 values (not 15).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Scrolling background loop however produces little gaps
« Reply #2 on: January 27, 2015, 11:49:17 pm »
Just tried

   
if (postion.x <= -576)
    {
        levelBackgroundSprites[i].setPosition(1152, postion.y);
    }
    else if (postion.x > 1152)
    {
        levelBackgroundSprites[i].setPosition(-576, postion.y);
    }

didn't seem to work :(

Update

In more testing and some small code changes it is working fine, however your comment did help me Hapax so cheers for that mate
« Last Edit: January 28, 2015, 12:05:26 am by Canvas »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Scrolling background loop however produces little gaps
« Reply #3 on: January 29, 2015, 01:30:33 am »
You're welcome.
I can see now that what I said didn't cover everything ;D

It might be wiser to set the position using a relative position rather than an absolute position, however, as it will then take into account the excess.
Simply replacing
.setPosition(1152, position.y); with .move(1728, 0);
and
.setPosition(-576, position.y); with move(-1728, 0);
should do the trick even when the comparisons were as they were before.

1728 is the width of all three sprites (576 x 3).

Also, I just spelled "position" correctly ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*