Time for action – bouncing Asteroids – part 2
Replace the current
Update()
method of theAsteroidManager
class:Public Sub Update(gameTime As GameTime) For Each asteroid as Sprite in Asteroids asteroid.Update(gameTime) If Not isOnScreen(asteroid) Then asteroid.Location=randomLocation() asteroid.Velocity=randomVelocity() End If Next For x as Integer = 0 to Asteroids.Count -1 For y as Integer = x+1 to Asteroids.Count -1 If (Asteroids(x).IsCircleColliding( Asteroids(y).Center, Asteroids(y). CollisionRadius)) Then BounceAsteroids(Asteroids(x), Asteroids(y)) End If Next Next End Sub
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 all of the...