I'm working on a simple 2D game that is similar to the classic Invader arcade. What I want to know is:

How can I draw a new sprite over the player sprite upon colliding with an enemy/a bullet? Better yet, how can I hide the player so that the hit animation can be shown?

I got the collision detection up and running except for the hit animation. It's being drawn behind the player current position which is awkward.

  • \$\begingroup\$ Maybe give us your draw code, then it would be easier to help. \$\endgroup\$
    – zacharmarz
    Mar 13 '12 at 7:17
  • \$\begingroup\$ Never mind everyone. I just got it. All I have to do is change from spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend) to spriteBatch.Begin(SpriteSortMode.Immediate,BlendState.AlphaBlend) \$\endgroup\$
    – Tommy
    Mar 13 '12 at 7:29
  • 1
    \$\begingroup\$ Try not to use Immediate, but rather use Defered. If it will work, it will be better. \$\endgroup\$
    – zacharmarz
    Mar 13 '12 at 8:16
  • 1
    \$\begingroup\$ "If it will work, it will be better." Incredibly overgeneralized statement that's not true for many scenes that use multiple render states. \$\endgroup\$ Mar 15 '12 at 20:11

Please investigate the parameters of the spriteBatch.Draw-method. Try not to feel intimidated by the sheer number of parameters of the largest one. The last parameter there is the "layerDepth" which should be a floating point value between 0 and 1. This parameter will be used (only) if you begin your drawing with SpriteSortMode.BackToFront or FrontToBack. If you make use of the layerDepth you don't have to worry to make the Draw()-calls in any particular order.

  • \$\begingroup\$ Is that the best approach? I tend to add a Z field and sort sprites by Z before rendering. \$\endgroup\$
    – ashes999
    Dec 31 '12 at 21:04

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.