2D XNA C#: Texture2D Wrapping Issue

Posted by Kieran on Game Development See other posts from Game Development or by Kieran
Published on 2011-10-23T20:40:53Z Indexed on 2011/11/27 2:03 UTC
Read the original article Hit count: 371

Filed under:

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:

Normal sprite

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

Infrequently they appear like this:

Wierd sprite

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

© Game Development or respective owner

Related posts about XNA