I'm drawing a ground texture in a sidescrolling platformer like this:
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, _camera.GetViewMatrix(Parallax));
foreach (Sprite sprite in Sprites)
sprite.Draw(spriteBatch);
spriteBatch.End();
In Sprite.Draw()
public void Draw(SpriteBatch spriteBatch)
{
if (Texture != null)
spriteBatch.Draw(Texture, Position, new Rectangle(0, 0, Texture.Width, Texture.Height), Color.White, Rotation, Origin, Scale, SpriteEffects.None, ZIndex);
}
How can I get the texture to repeat in the X Direction infinitely?
Any resources explaining how to do this would be great. Thanks!
My implementation of camera is based on this: http://www.david-gouveia.com/2d-camera-with-parallax-scrolling-in-xna/
EDIT
This is what I tried, I made a separate draw for the background like this:
//Draw Background
spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.LinearWrap, null, null,null, camera.GetViewMatrix(Vector2.One));
groundSprite.Draw(spriteBatch, (int)camera.Position.X - (int)camera.Origin.X / 2);
spriteBatch.End();
and in Sprite.Draw():
public void Draw(SpriteBatch spriteBatch, int scrollX = 0)
{
if (Texture != null)
spriteBatch.Draw(Texture, Position, new Rectangle(scrollX, 0, Texture.Width, Texture.Height), Color.White, Rotation, Origin, Scale, SpriteEffects.None, ZIndex);
}
still have some issues.


Second Edit
It just came to me. I had the parallax set to 1 on the ground. It should be 0 so that it never actually moves. It just looks like it moves when you use the answer I selected below.
Thanks!
Don't scroll the sprite in space; scroll the texture coordinates used to render it.
Set your sampler to wrap its UV coordinates:
spriteBatch.Begin(..., SamplerState.LinearWrap, ...);
Then specify a source rectangle when rendering:
var source = new Rectangle(scrollX, 0, texture.Width, texture.Height);
spriteBatch.Draw(texture, destination, source, Color.White);
If the resulting UV coordinates fall off the edge of the texture, they will wrapped by the texture sampler, which should give you the desired "scrolling" effect.
Draw the background twice in the Sprite Draw method, but offset one of them, something like this (untested):
spriteBatch.Draw(Texture, Position, new Rectangle(0, 0, Texture.Width, Texture.Height), Color.White, Rotation, Origin, Scale, SpriteEffects.None, ZIndex);
spriteBatch.Draw(Texture, Position-new Vector2(Texture.Width, 0), new Rectangle(0, 0, Texture.Width, Texture.Height), Color.White, Rotation, Origin, Scale, SpriteEffects.None, ZIndex);
And then in the update method:
if (Position.X > Texture.Width) {
Position.X -= Texture.Width;
}
else if (Position.X < 0) {
Position.X += Texture.Width;
}
来源:https://stackoverflow.com/questions/8352587/infinite-scrolling-background-in-xna