0
\$\begingroup\$

When I added a Rectangle to the Draw method for the sprite, it did not show up on screen.

 spriteBatch.Draw(texture, position, new Rectangle((int)position.X, (int)position.Y, (int)texture.Width, (int)texture.Height), Color.White, rotation, orgin, scale, SpriteEffects.None, 0f);
\$\endgroup\$
1
  • \$\begingroup\$ have you called spriteBatch.begin() and spriteBatch.end()? \$\endgroup\$ Jul 13 '12 at 9:58
2
\$\begingroup\$

The third parameter specifies which parts of the sprite you wish to draw.

From MSDN:

sourceRectangleType: Rectangle?

A rectangle that specifies (in texels) the source texels from a texture. Use null to draw the entire texture.

You should change your code to something like this:

  spriteBatch.Draw(texture, position, new Rectangle(0, 0, (int)texture.Width, (int)texture.Height), Color.White, rotation, orgin, scale, SpriteEffects.None, 0f);    

This parameter is useful if you're using spritesheets (image files with multiple sprites in them).

\$\endgroup\$
2
  • \$\begingroup\$ Thanks! It works! But while I searched the net, I found some info about problems with collsion detection if the object i rotation? Is the rectangle also rotation? All my objects a like a square with equal width and height, will this not work? \$\endgroup\$
    – 3D-kreativ
    Jul 13 '12 at 10:18
  • \$\begingroup\$ Well, that's a whole different question. It really depends on how you manage your collisions. This Rectangle is independent from the sprite's MinimalBoundingRectangle. If you have problems regarding rotation, collision detection and MBR's, then use the search. \$\endgroup\$
    – Marton
    Jul 13 '12 at 10:46

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.