Jump to content
• Advertisement

Public Group

# Managing grids of enemies?

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

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I'm working on a game in the style of Galaxian and I have reached an impasse. I don't know how to go about setting up a grid of sprites. The way I have it set up right now is a for loop with a nested for loop to cycle through the rows and columns and then draw each sprite in it's place a little ways from the last one. The problem is that all the sprites show up all clumped together. How can I fix that?

#### Share this post

##### Share on other sites
Advertisement
Must be some mistake with the logic in how the for loops place the sprites as they iterate I imagine.

If you posted the loops here, it'd probably be easy to see what's wrong.

#### Share this post

##### Share on other sites

The way I have it set up right now is a for loop with a nested for loop to cycle through the rows and columns and then draw each sprite in it's place a little ways from the last one.

Try increasing the "little ways from the last one".

If you're trying make a grid of something, a nested for loop sounds right, but if you're trying to space them out more (like you said), just try to increase the distance in each for loop.

For example if you've got something like this:

 for (row = 0; row < 10; row++) { for (col = 0; col < 10; col++) { drawEnemy(row, col); } } 

Then you could change the following line from

 drawEnemy(row, col) 

to

 drawEnemy(10*row, 10*col); 

You will very easily be able to see the difference, and you can adjust the factor (of 10 in this case), accordingly.

#### Share this post

##### Share on other sites
Alright, I tried your idea and I now have a grid of aliens but they are spaced too far apart.

#### Share this post

##### Share on other sites

Alright, I tried your idea and I now have a grid of aliens but they are spaced too far apart.

um, what happens if you use like 3 instead of 10? Or maybe 2/4/3.5/2.6546876846876846875877831

#### Share this post

##### Share on other sites
Maybe it's a scale problem. Using the grid indice directly as pixel position is not a good idea. Maybe you can simply multiply the grid indice by the size of your enemy sprite and eventually with some spacing.

#### Share this post

##### Share on other sites
I think the problem might be with the size of my bounding rectangle. Is it possible to use Spritebatch.Draw with a vector2 rather than a rectangle? It throws me an error when I do.

#### Share this post

##### Share on other sites
I fixed my problem with the size of the intervals, but now I don't know how to keep the sprites on screen. I tried checking if they were over a certain x coordinate and I tried checking if they were over the window bounds but neither of these seems to work.

#### Share this post

##### Share on other sites

I fixed my problem with the size of the intervals, but now I don't know how to keep the sprites on screen. I tried checking if they were over a certain x coordinate and I tried checking if they were over the window bounds but neither of these seems to work.

So, is the problem that the sprites' movement is carrying them offscreen, rather than changing directions once they reach the edge? If so, then either of the above approaches will work-- there must be a problem with your implementation if it isn't working now. Posting code will help us see where that might be. If that's not the problem that you're having, then please clarify.

#### Share this post

##### Share on other sites
Here is the code. Basically I have it check for either of the sides.

 private void CheckWallColl() { if ((alienVelocity > 0 && spaceShip_Alien_WeakX < Window.ClientBounds.Width) || (alienVelocity > 0 && spaceShip_Alien_WeakX > 0)) { float velocityChange = 1.01f; alienVelocity = alienVelocity * (int)-velocityChange; } } 

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
12
• Advertisement

• 9
• 21
• 13
• 9
• 17
• ### Forum Statistics

• Total Topics
632608
• Total Posts
3007391

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!