Time for action – checking the asteroid's position
Add the
isOnScreen()
helper method to the AsteroidManager class:private bool isOnScreen(Sprite asteroid) { if (asteroid.Destination.Intersects( new Rectangle( -screenPadding, -screenPadding, screenWidth + screenPadding, screenHeight + screenPadding) ) ) { return true; } else { return false; } }
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.