BOMEz
BOMEz

Reputation: 1010

Scrolling background not smooth

I'm following this tutorial attempting to learn XNA, but I'm having issues making my images scroll smoothly. Everything works fine until it's time for my 1st image to come back round and start the scroll loop again.

What's weird is that the issue seems to be with drawing the image as it takes a few seconds, and then pops into view right where you would expect it to be if it were to continue scrolling. I changed the background to red to see if the image wasn't loading for some reason, but I get a continuous stream of the same blue color used in the background sprite texture.

Any suggestions? Below is the code for the update method:

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here
        if (bg1.getPosition().X < -bg1.size.Width)
        {
            bg1.setPosition( new Vector2(bg5.getPosition().X + bg5.size.Width,0) );
        }
        if (bg2.getPosition().X < -bg2.size.Width)
        {
            bg2.setPosition(new Vector2(bg1.getPosition().X + bg1.size.Width, 0));
        }
        if (bg3.getPosition().X < -bg3.size.Width)
        {
            bg3.setPosition(new Vector2(bg2.getPosition().X + bg2.size.Width, 0));
        }
        if (bg4.getPosition().X < -bg4.size.Width)
        {
            bg4.setPosition(new Vector2(bg3.getPosition().X + bg3.size.Width, 0));
        }
        if (bg5.getPosition().X < -bg5.size.Width)
        {
            bg5.setPosition(new Vector2(bg4.getPosition().X + bg4.size.Width, 0));
        }

        Vector2 direction = new Vector2(-1,0);
        Vector2 speed = new Vector2(100.0f/1000.0f,0.0f);

        bg1.updatePosition( (direction * speed) * (float)gameTime.ElapsedGameTime.TotalMilliseconds);
        bg2.updatePosition((direction * speed) * (float)gameTime.ElapsedGameTime.TotalMilliseconds);
        bg3.updatePosition((direction * speed) * (float)gameTime.ElapsedGameTime.TotalMilliseconds);
        bg4.updatePosition((direction * speed) * (float)gameTime.ElapsedGameTime.TotalMilliseconds);
        bg5.updatePosition((direction * speed) * (float)gameTime.ElapsedGameTime.TotalMilliseconds);

        base.Update(gameTime);
    }

Upvotes: 0

Views: 218

Answers (1)

jgallant
jgallant

Reputation: 11273

Looking at your if statements, at some point it will break.

You are moving all of your images to the left. Then when it reaches itself in width, it resets itself based on another image's position, which is also moving to the left.

This logic is flawed, and needs to be revised.

Upvotes: 1

Related Questions