# 2D XNA C#: Texture2D Wrapping Issue

Working in C#/XNA for a Windows game: I'm using Texture2D to draw sprites. All of my sprites are 16 x 32. The sprites move around the screen as you would expect, by changing the top X/Y position of them when they're being drawn by the spritebatch.

Most of the time when I run the game, the sprites appear like this:

and when moved, they move as I expect, as one element.

Infrequently they appear like this:

and when moved it's like there are two sprites with a gap in between them - it's hard to describe.

It only seems to happen sometimes - is there something I'm missing? I'd really like to know why this is happening.

[Edit:] Adding Draw code as requested:

This is the main draw routine - it first draws the sprite to a RenderTarget then blows it up by a scale of 4:

protected override void Draw(GameTime gameTime)
{
// Draw to render target
GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.Clear(Color.CornflowerBlue);
Texture2D imSprite = null;

spriteBatch.Begin(SpriteSortMode.FrontToBack, null, SamplerState.PointWrap, null, null);
ManSprite.Draw(spriteBatch);
base.Draw(gameTime);
spriteBatch.End();

// Draw render target to screen
GraphicsDevice.SetRenderTarget(null);

imageFrame = (Texture2D)renderTarget;

GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DarkSlateBlue, 1.0f, 0);
spriteBatch.Begin(SpriteSortMode.FrontToBack, null, SamplerState.PointClamp, null, null);
spriteBatch.Draw(imageFrame, new Vector2(0, 0), null, Color.White, 0, new Vector2(0, 0), IM_SCALE, SpriteEffects.None, 0);
spriteBatch.End();

}


This is the draw routine for the Sprite class:

public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, new Vector2(PositionX, PositionY), null, Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0.3f);
}

• Something like this is really hard to debug. I'd try swapping the texture with a debug one, something patterned such that you can easily identify where parts of it are rendered. – George Duckett Oct 23 '11 at 21:23
• One common mistake when sprites aren't moving correctly is because there's inconsistency between when you move it with .Update logic and when you move it with .Draw logic. Generally you should always avoid doing any engine logic in .Draw and only move and position things etc. in the .Update method. – William Mariager Oct 23 '11 at 21:24
• @MindWorX this doesn't seem like a simple problem - It's more like the texture is rolling around the sprite rather than the sprite moving incorrectly – Kieran Oct 23 '11 at 21:43
• Can you post your draw code? – Benixo Oct 23 '11 at 22:12
• I don't think you should be calling base.Draw(...) right in the middle like that. – Blecki Oct 24 '11 at 12:58

(If you haven't solved it already)

Two things you can do to debug this further (if my solutions are not your problem) and give us more information:

1. Use a normal SpriteBatch.Begin. (i.e. Just have a SpriteSortMode and a BlendState. No PointWrap (which I think may be your problem since you're scaling it.) If that doesn't work then:

2. Comment out the Rendertarget code and render normally. I've seen some of my Rendertargets do wonky stuff like this, so I wouldn't rule it out.

Also, @Blecki, base.Draw simply draws something tagged as a DrawableGameComponent. All the code is doing there is drawing the rendertarget on top of any other automatic draw calls. That shouldn't be a problem.

I would bet my money on the PointWrap, as it will "wrap" the sprite when it is scaled if it cannot scale it uniformly.

• thanks, good idea, I'll give that a try later tonight. :) – Kieran Nov 28 '11 at 17:47

I eventually resolved this issue; it was to do with a faulty graphics chipset in the computer I was developing on. A new graphics card was required.