Time for action – checking the asteroid's position
Add the
isOnScreen()
helper method to theAsteroidManager
class:Private Function isOnScreen(asteroid As Sprite) As Boolean If (asteroid.Destination.Intersects( new Rectangle( -screenPadding, -screenPadding, ScreenWidth + screenPadding, screenHeight + screenPadding) ) ) Then Return True Else Return False End If End Function
What just happened?
The isOnScreen()
method checks the passed asteroid to determine if its destination rectangle (where the sprite would be drawn to the screen) intersects a rectangle, generated by expanding the screen by screenPadding
pixels in all directions. This padding allows the asteroid to move several pixels off the screen, before the game determines that it has actually left the playfield.