Managing grids of enemies?

Started by
8 comments, last by jniblick 13 years, 1 month ago
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?
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.

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.
Alright, I tried your idea and I now have a grid of aliens but they are spaced too far apart.

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
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.
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.
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.

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.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

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;
}
}

This topic is closed to new replies.

Advertisement