3
\$\begingroup\$

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);
}
\$\endgroup\$
9
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 23, 2011 at 21:23
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – William
    Commented Oct 23, 2011 at 21:24
  • \$\begingroup\$ @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 \$\endgroup\$
    – Kieran
    Commented Oct 23, 2011 at 21:43
  • \$\begingroup\$ Can you post your draw code? \$\endgroup\$
    – Benixo
    Commented Oct 23, 2011 at 22:12
  • 2
    \$\begingroup\$ I don't think you should be calling base.Draw(...) right in the middle like that. \$\endgroup\$
    – Blecki
    Commented Oct 24, 2011 at 12:58

2 Answers 2

1
\$\begingroup\$

(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.

\$\endgroup\$
1
  • \$\begingroup\$ thanks, good idea, I'll give that a try later tonight. :) \$\endgroup\$
    – Kieran
    Commented Nov 28, 2011 at 17:47
0
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .