[XNA] spriteBatch.Draw Error

Started by G_G, April 23, 2010, 11:59:55 pm

Previous topic - Next topic

G_G

This is how I'm drawing a sprite right now.
spriteBatch.Draw(tex, i.location, null, Color.White, i.Angle(), new Vector2(0, 0), SpriteEffects.None, 100f);

Pay attention to the i.location.

I get this error when I try to run.
Error	2	Argument '2': cannot convert from 'Microsoft.Xna.Framework.Vector2' to 'Microsoft.Xna.Framework.Rectangle'	D:\Users\Ronnie\Documents\Visual Studio 2008\Projects\GameTests\GameTests\Game1.cs	85	39	GameTests


Its saying I need a rectangle where i.location is even though its supposed to be a vector2.

Any help is appreciated I don't get why it won't work at all. Its giving me a massive headache.

Here's my complete draw method.

           spriteBatch.Begin();
           foreach (Bullet i in bullets)
           {
               Texture2D tex = Content.Load<Texture2D>("bullet");
               spriteBatch.Draw(tex, i.location, null, Color.White, i.Angle(), new Vector2(0, 0),
                   SpriteEffects.None, 100f);
           }
           // TODO: Add your drawing code here
           spriteBatch.End();


I can get it to work like this.
spriteBatch.Draw(tex, i.location, null, Color.White);[code]
[/code]

Ryex

April 24, 2010, 01:21:23 am #1 Last Edit: April 24, 2010, 02:08:40 am by Ryexander
Quote from: game_guy on April 23, 2010, 11:59:55 pm

(tex, i.location, null, Color.White, i.Angle(), new Vector2(0, 0), SpriteEffects.None, 100f);



why do you have two vectors?
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

winkio

April 24, 2010, 01:29:32 am #2 Last Edit: April 24, 2010, 02:53:43 am by winkio
To fix the error, add a scale value (you probably want 1.0f) before sprite effects.  You left out an argument, so it thought you were calling a different part of the overloaded method.

Also, a tip:  Vector2.Zero is your friend.

And one more thing that I didn't notice before:  don't load content in Draw.  Or Update, or anything after initialization really.  Load all the content you need in your constructor (or LoadContent, which should be called from the constructor in most classes) and store it in local variables.

G_G

April 24, 2010, 10:27:43 am #3 Last Edit: April 24, 2010, 10:29:22 am by game_guy
Okay I got it to run with no error using this.
spriteBatch.Draw(tex, i.location, null, Color.White, i.Angle(), Vector2.Zero, 
                   Vector2.Zero, SpriteEffects.None, 100f);


Now it won't draw the sprite in the upper corner where it should be.

EDIT: Here's the new code I used
spriteBatch.Draw(tex, i.location, null, Color.White, i.Angle(), Vector2.Zero, 1.0f,
                     SpriteEffects.None, 100f);


And it still won't draw anything.

winkio

spriteBatch.Draw(tex, Vector2.Zero, null, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1f);


test that and see what happens.  If it still doesn't draw, then it's something outside of this region of code that's affecting it.  If it does draw, then just check and make sure i.Angle and i.location are reasonable.

G_G

I fixed it. the location ended up being negative for some reason. O_O