The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

Invisable sprite RRS feed

  • General discussion

  • Ok i am stuck i have a block of code that will work on every sprite other then the player sprite when im drawing it can anyone help me out.

                spritebatch.Draw(texture, rect, Color.Red); <--- Will draw player;  
                Vector2 vec = Vector2.Zero;  
                vec.X = rect.Width / 2;  
                vec.Y = rect.Height / 2;  
                spritebatch.Draw(texture, rect, rect, Color.Red, 0, vec, SpriteEffects.None, 1); <-- Will not draw player; I have this line for all of my enemies and they are getting drawn but why not this guy? 
    Its really weird is this a bug with the emulator or xna i have no idea why this is happening;
    Monday, January 23, 2012 9:27 PM

All replies

  • The second rect parameter is supposed to be the source rectangle within the texture, not a screen location...
    Monday, January 23, 2012 9:33 PM
  • So why does it draw out my enemys then? Can I put NULL for the source rect there is no animations going on in my game;
    Monday, January 23, 2012 9:59 PM
  • Read the documentation on the method you're using. There's a whole slew of possible problems with your code.

    This is the method signature:

    public void Draw (  
             Texture2D texture,  
             Vector2 position,  
             Nullable<Rectangle> sourceRectangle,  
             Color color,  
             float rotation,  
             Vector2 origin,  
             Vector2 scale,  
             SpriteEffects effects,  
             float layerDepth  

    You have sourceRectangle to the same as your position, which is probably going to be incorrect. If you aren't using a spritesheet, you can pass null here.

    You have a scale set to half of the rectangles width/height, which I'm guessing is not what you want to do. If you pass in 1 (as a float), it'll scale it to the original texture size. I haven't used the Vector overload, but I'm assuming new Vector(1,1) would be the equivalent (although don't quote me on that).

    You have the layerDepth of 1. In most cases your sprite is going to be draw behind any other objects that you might be drawing on the screen at the same time.

    Unless you specifically need to use the more complex overload, just stick to the simpler one.
    Tuesday, January 24, 2012 3:05 PM
  • Actually, he's using this overload, which takes eight parameters (e.g. no scale parameter). As you mentioned, he should probably be passing null for the source rectangle. But the Vector parameter is the sprite origin, so it might be ok (I've never used an origin with a destination rectangle). You're also probably spot-on with the layerDepth parameter.

    Good catch. I must've gotten them muddled together.
    Tuesday, January 24, 2012 4:32 PM
  • Hey thanks for the info guys and girls. I changed my new rect origion to

    vector2 vec =;  
    vec.X = texture.Width/2;  
    vec.Y = texture.Height/2;  
    spritebatch.Draw(texture, rect, new Rectangle(0, 0, texture.Width, texture.Height), Color.Green, 0f, vec, SpriteEffects.None, 1);  
    All sprites seam to be drawing perfectly now but i have a question though.

    If i used the standerd spritebatch.draw(texture,rect,color.Red);
    it would draw the sprite at 0,0 but when i set the origion to the center of the sprite it seams to be drawing at rect.X + rect.Width/2 and rect.Y + rect.Height/2
    is there an easy fast way to fix this other then for me to translate the rect back to its origional origion?
    Wednesday, January 25, 2012 6:29 PM