Chaos Project

Game Development => Sea of Code => Topic started by: G_G on April 23, 2010, 11:59:55 pm

Title: [XNA] spriteBatch.Draw Error
Post by: G_G on April 23, 2010, 11:59:55 pm
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]
Title: Re: [XNA] spriteBatch.Draw Error
Post by: Ryex on April 24, 2010, 01:21:23 am
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?
Title: Re: [XNA] spriteBatch.Draw Error
Post by: winkio on April 24, 2010, 01:29:32 am
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.
Title: Re: [XNA] spriteBatch.Draw Error
Post by: G_G on April 24, 2010, 10:27:43 am
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.
Title: Re: [XNA] spriteBatch.Draw Error
Post by: winkio on April 24, 2010, 01:54:56 pm
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.
Title: Re: [XNA] spriteBatch.Draw Error
Post by: G_G on April 24, 2010, 02:49:13 pm
I fixed it. the location ended up being negative for some reason. O_O