Time for action – bouncing asteroids – part 2
Replace the current
Update()
method of the AsteroidManager class:public void Update(GameTime gameTime) { foreach (Sprite asteroid in Asteroids) { asteroid.Update(gameTime); if (!isOnScreen(asteroid)) { asteroid.Location=randomLocation(); asteroid.Velocity=randomVelocity(); } } for (int x = 0; x < Asteroids.Count; x++) { for (int y = x + 1; y < Asteroids.Count; y++) { if (Asteroids[x].IsCircleColliding( Asteroids[y].Center, Asteroids[y]. CollisionRadius)) { BounceAsteroids(Asteroids[x], Asteroids[y]); } } } }
Execute the game and watch the asteroids bounce off of each other.
Close the game window.
What just happened?
Each asteroid is updated just as before. Afterwards, however, two nested loops process each asteroid in the list, checking for collisions with...