Time for action – checking the asteroid's position

  1. 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.

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.