3
\$\begingroup\$

So I have a TileSprite class that makes a TileSpriteobject with a collision box in it named bCollisionRect:

public TileSprite(Texture2D textureImage, Vector2 position, Point frameSize, Point currentFrame)
        : base (textureImage, position, frameSize, currentFrame)
{
    bCollisionRect = new Rectangle((int)position.X + collisionOffSet, (int)position.Y + collisionOffSet, frameSize.X - (collisionOffSet * 2), frameSize.Y - (collisionOffSet * 2));
}

But when I try to draw the collision boxes (to test collision detection), it doesn't draw the box. I tried using this:

public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
    // This is what I tried:
    spriteBatch.Draw(textureImage, bCollisionRect, Color.White);

    // base.Draw draws the texture (.Draw method from another abstract class)
    base.Draw(gameTime, spriteBatch);
}

I use this line of code in another class to draw my tiles (pure for testing!):

tilename.Draw(gameTime, spriteBatch);
\$\endgroup\$
  • \$\begingroup\$ What do you mean by "It doesn't work"? Is the sprite drawn at a different location than expected? Is the sprite not drawn at all? \$\endgroup\$ – jgallant Nov 4 '15 at 17:29
  • \$\begingroup\$ Not drawn at all, sorry for the confusion \$\endgroup\$ – Jelle Nov 4 '15 at 17:31
  • \$\begingroup\$ frameSize.X - (collisionOffSet * 2) Are you sure this is not resulting in 0? What is collisionOffset? This seems incorrect to me. Try setting a new Rectangle(position.x, position.y, 100,100) and see if it draws. \$\endgroup\$ – jgallant Nov 4 '15 at 17:33
  • \$\begingroup\$ I think I found my problem. I use textureImage but that just draws the tile again(I saw this when I used your values, because I saw a little tile, the same as the tile I drew). So do I need to use a texture for the collision box as textureImage? \$\endgroup\$ – Jelle Nov 4 '15 at 17:38
  • \$\begingroup\$ Yep, use a blank texture for debugging purposes. I typically have a 1x1 white texture I generate at runtime. \$\endgroup\$ – jgallant Nov 4 '15 at 17:39
3
\$\begingroup\$

Code to create a blank texture at runtime (after discussing with you via comments above):

Texture2D blankTexture = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
Color[] color = new Color[1];
color[0] = Color.White;
blankTexture.SetData(color);

Then your draw call... specify a color if you want to tint it:

spriteBatch.Draw(blankTexture, bCollisionRect, Color.Red);
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.