# [XNA] Positioning (logic)

This topic is 2843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have an array of textures:

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

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 11
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632648
• Total Posts
3007642
• ### Who's Online (See full list)

There are no registered users currently online

×