-2

I have a texture with 20x20 pixels.

Consider this piece of code:

public override void Draw(GameTime gameTime)
{
    spriteBatch.Draw(texture,Position * 20,null, Color.White, 0f, new Vector2(texture.Width / 2, texture.Height / 2),1, SpriteEffects.None,1);
    base.Draw(gameTime);
}

Position=(0,0)

enter image description here

But when I use new Vector2(10,10) in the following code

public override void Draw(GameTime gameTime)
{
    spriteBatch.Draw(texture,Position * 20+new Vector2(10,10),null, Color.White, 0f, new Vector2(texture.Width / 2, texture.Height / 2),1, SpriteEffects.None,1);
    base.Draw(gameTime);
}

It draws at the correct position. How can I fix this?

Alex
  • 876
  • 12
  • 28
  • Not sure what makes that position correct. Are you trying to position the texture different from where it is drawn, which starts at the upper left? – jzx Dec 06 '14 at 05:29

3 Answers3

0

The line we're interested in is

Position * 20

which, if Position is (0,0), is equal to (0,0)

and you say it behaves correctly for

Position * 20 + new Vector2(10,10)

which is equal to (10,10)

I'm guessing that the problem is a misunderstanding of order of operation. Adding parenthesis to show what happens first, we would have:

(Position * 20) + new Vector2(10,10)
jzx
  • 3,805
  • 2
  • 23
  • 38
  • public override void Draw(GameTime gameTime) { spriteBatch.Draw(texture, new Vector2(0,0), null, Color.White,0f,new Vector2(texture.Width/2,texture.Height/2), 1f, SpriteEffects.None, 1f); base.Draw(gameTime); } so I edited it and the same, lack of 10pixel – Thuc Vu Van Dec 06 '14 at 06:14
0

This is a debugging issue. The image gets drawn where you are telling it to be drawn. The problem is you are not telling it to draw in the correct place. Use the debugger to examine Position when you enter the Draw() method. My guess is that Position will not have the value that you are expecting. If this is the case, look through your code, and find where Position is being set, or where it is being changed, and make sure those bits of code are doing what they are suppose to do.

Aholio
  • 1,206
  • 8
  • 11
0

It's unclear what you're asking, but based on everything you stated I don't see any issues.

Position is (0, 0), the texture dimensions are 20 x 20 and the origin which you specified in the Draw method is new Vector2(texture.Width / 2, texture.Height / 2).

What I see in the screenshot appears to be correct, with the top left corner of the texture being at (0, 0) after specifying its origin to be (10, 10) and moving it by (10, 10).

RecursiveCall
  • 566
  • 4
  • 13