Sign in to follow this  
VitaliBR

[XNA] Positioning (logic)

Recommended Posts

I have an array of textures:


I am placing them so
            for (int i = 0; i < 6; i++)
spriteBatch.Draw(balloon[i], new Vector2((190 + i * 70), 50), Color.White);
for (int i = 6; i < 12; i++)
spriteBatch.Draw(balloon[i], new Vector2((i * 70) - 230, 137), Color.White);
for (int i = 12; i < 18; i++)
spriteBatch.Draw(balloon[i], new Vector2((i * 70) - 650, 224), Color.White);
for (int i = 18; i < 24; i++)
spriteBatch.Draw(balloon[i], new Vector2((i * 70) - 1070, 311), Color.White);
for (int i = 24; i < 30; i++)
spriteBatch.Draw(balloon[i], new Vector2((i * 70) - 1490, 398), Color.White);
for (int i = 30; i < 36; i++)
spriteBatch.Draw(balloon[i], new Vector2((i * 70) - 1910, 485), Color.White);




I'm having doubts as to my "selector" that yellow square, walking on the "table"
I positioned it there manually:
spriteBatch.Draw(selection_v, new Vector2(172, 33), Color.White);



and gave those numbers broken (172,33) :(

What is the best method to do it correctly positioning himself, and I can for example press to the right and he goes to the two balloons on the right?

Thanks

Share this post


Link to post
Share on other sites
First, you're hardcoding a lot of numbers. A good programming practice is to store data in just one place. E.g., the location of the upper left-hand corner of your array should be stored in a Vector2 or as 2 floats such as ArrayX and ArrayY. In addition, store the width of your balloon in a variable, rather than using a hard-coded number. Most of your other numbers should be stored in variables, also.

If you do that, then you can position the yellow box by keeping track of the yellow box's row and column.

E.g. (pseudo-code),

Vector2 upperLeft(190,50);
float columnWidth = 70;
float rowHeight = 87;
Vector2 yellowBoxLoc = upperLeft + Vector2( userX*columnWidth, userY*rowHeight);
spriteBatch.Draw(selection_v, yellowBoxLoc, Color.White);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this