Public Group

# Asteroids clone.. asteroids won't shatter

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

## Recommended Posts

Hello,

My first post on this forum. Hope folks here will help me in some way.

The problem is this: I am completely new to programming and I recently implemented lists in my XNA code in order to minimize my code. There are two lists, one for big asteroids and another list for small asteroids. My idea is that when one asteroid is shot (collision already works btw), 4 smaller asteroids should spawn at that position. I reckon the position-tracking is already functional, so the main issue is to get the small asteroids to actually spawn @collision.

I want to solve this by not having a lot of small asteroids in the list to begin with, but by adding(!) new rocks at the moment of collision. So when you hit a normal asteroid, the smallAsteroid-list should increase by 4 smallAsteroids and then draw those 4 asteroids. This does not happen and I can't figure out how I should write the code.

Game1.cs
http://codeviewer.org/view/code:1e84

SmallAsteroid.cs (class for the small asteroids)
http://codeviewer.org/view/code:1e85

Thanks

##### Share on other sites
The conditional in the small asteroid for loop in Update() is backwards.

for (int i = 0; i > sRockList.Count; i++) { sRockList.Update(gameTime, clientBounds, rockList.mPos); }
It should be "<".

##### Share on other sites

The conditional in the small asteroid for loop in Update() is backwards.

for (int i = 0; i > sRockList.Count; i++) { sRockList.Update(gameTime, clientBounds, rockList.mPos); }
It should be "<".

Thanks, I fixed that.

Does not change my problem though.

I tried writing the following in the Draw code:

 for (int i = 0; i < 4; i++) { if (rockList.explode == false) { SmallAsteroid temp = new SmallAsteroid(gameRandom); temp.contentLoad(this.Content, ("Images/smallasteroid")); sRockList.Add(temp); sRockList.Draw(spriteBatch); } }

In that it should draw 4 small asteroids and add the texture to all of those with "temp".
However, when I try and run the game, it points to "clientbounds" in

"srocklist.Update(gameTime, clientBounds, rockList.mPos);"

and says

"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"

##### Share on other sites
[font="Lucida Console"]
for (int i = 0; i < sRockList.Count; i++)
{
sRockList.Update(gameTime, clientBounds, rockList.mPos);
}
[/font]
Note the mismatch between sRockList and rockList used here.

##### Share on other sites

[font="Lucida Console"]
for (int i = 0; i < sRockList.Count; i++)
{
sRockList.Update(gameTime, clientBounds, rockList.mPos);
}
[/font]
Note the mismatch between sRockList and rockList used here.

Yes, its been noted. How else should I access the rocklist.mPos?

I tried this:

 for (int i = 0; i < rockList.Count; i++) { foreach (SmallAsteroid sRock in sRockList) { sRock.Update(gameTime, clientBounds, rockList.mPos); } rockList.Update(gameTime, clientBounds); }

spoiler: Did not work.

##### Share on other sites
Define "did not work".

Your code (in the links you posted earlier) seems to interchange between rockList and sRockList at inappropriate times. Here is another case:
 for (int i = 0; i < sRockList.Count; i++) { if (rockList.explode == true) { sRockList.Draw(spriteBatch); } } 
Using more foreach loops would prevent many of these bugs. Use for(int i...) loops only if you need the index for some reason.

##### Share on other sites
Why do you only draw the small rocks if they have exploded == true?

Here's some general tips for debugging this: You want to confirm with the debugger that several things are happening. First, that four small rocks are being made. Second, that those rocks are being added to the list. Third, that when you go to the game's draw loop, you are infact looping over those boxes. And lastly, you want to confirm that the drawing code is correct (eg: by starting with some small rocks already initialized and seeing if they draw).

##### Share on other sites
I think what you should do is get rid of the explode member altogether. Then when you detect that the large asteroid should explode, you create 4 new small asteroids at the large asteroid's position (by taking a copy of the value at the moment of explosion) and insert them in the small asteroid list. Then, you should remove the large asteroid from the large asteroid list.

I think what you're trying to do with the mixed up references to small and large asteroids in the same loop is reference the position of the large asteroid even after the explosion, which isn't the way to do it.

Incidentally, the 3rd value (the large asteroid position) you're passing into SmallAsteroid.Update() isn't even used in that procedure, so you can remove it anyway.

Technically speaking you could probably get away with just one Asteroid class if you have a mechanism for storing the size. You could store the size as an Integer value and divide the value by 4 each time you split it into smaller ones and have more than 2 sizes.

For example, starting with an asteroid of mass=128, after the explosion create 4 small asteroids each with mass=32, then if any of them are exploded, spawn 4 replacements with mass = 4, and so on..

Depending on your choice of initial mass you can have as many levels as you want, or do more interesting break ups of the asteroids by having them not all be of equal mass.

• 10
• 13
• 14
• 18
• 15