Search code examples
c#silverlight-4.0xnaxna-4.0

How to redirect to page in Silverlight from XNA Page?


What i'm trying to do is redirect from XNA GamePage.xaml to some other page in Silverlight.

For example, once the player has no more lives, I want to display a Silverlight Page with the text GAME OVER. How can I do that? I tried something like this in onUpdate method:

if(lifes == 0)
{
    SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
    timer.Stop();
    NavigationService.Navigate( new Uri("/GameOverPage.xaml",UriKind.Relative));
}

but this always give me an error. How should it be done to work?

thanks for advance:)


Solution

  • Here is the right method !

    Create a class GameOverScreen:

    public class GameOverScreen
    {
    private Texture2D texture;
    private Game1 game;
    private KeyboardState lastState;
    
    public GameOverScreen(Game1 game)
    {
        this.game = game;
        texture = game.Content.Load<Texture2D>("GameOverScreen");
        lastState = Keyboard.GetState();
    }
    
    public void Update()
    {
        KeyboardState keyboardState = Keyboard.GetState();
    
        if (keyboardState.IsKeyDown(Keys.Enter) && lastState.IsKeyUp(Keys.Enter))
        {
            game.StartGame();
        }
        else if (keyboardState.IsKeyDown(Keys.Escape) && lastState.IsKeyUp(Keys.Escape))
        {
            game.Exit();
        }
    
        lastState = keyboardState;
    }
    
    public void Draw(SpriteBatch spriteBatch)
    {
        if (texture != null)
            spriteBatch.Draw(texture, new Vector2(0f, 0f), Color.White);
    }
    }
    

    Implementing the GameOverScreen class

    Now that we have a GameOverScreen class we need to add code to Game1.cs to implement it.

    First we need a variable for the new screen. At the top of the the Game1 class add a new

    GameOverScreen object:
    StartScreen startScreen;
    GamePlayScreen gamePlayScreen;
    GameOverScreen gameOverScreen;
    

    Next we need to add a case to the switch statement in the Game1.Update() method for the GameOverScreen:

    case Screen.GameOverScreen:
    if (gameOverScreen != null)
        gameOverScreen.Update();
    break;
    

    And we have to do the same for the Draw() method:

    case Screen.GameOverScreen:
    if (gameOverScreen != null)
        gameOverScreen.Draw(spriteBatch);
    break;
    

    Now we need to add an EndGame() method that will close the GamePlayScreen and open the GameOverScreen. This will be called when the game over condition is met.

    public void EndGame()
    {
    gameOverScreen = new GameOverScreen(this);
    currentScreen = Screen.GameOverScreen;
    
    gamePlayScreen = null;
    }
    

    One minor change needs to be made to the StartGame() method as well. On the GameOverScreen we are going to give the user the option to restart the game, which will call the StartGame() method. So at the end of the StartGame() method we just need to add one line of code to set gameOverScreen to null.

     gameOverScreen = null;
    

    Game Over Condition

    The last thing we need to do is take care of the game over condition, which will be handled in the GamePlayScreen class. Open GamePlayScreen.cs. The first thing we need here is a new integer to hold the amount of lives the player has, add this to the top of the class: For example:

    int lives = 3;
    

    This values doesn’t have to be 3, you can change it to whatever you like of course. Next we need to add the code to decrement lives every time a slice of cake moves off the bottom of the screen and is removed. When lives is equal to 0, Game1.EndGame() will be called. This code will be added to the HandleFallingCake() method.

    if (toRemove.Count > 0)
    {
    foreach (Sprite cake in toRemove)
    {
        cakeList.Remove(cake);
        --lives;
        if (lives == 0)
            game.EndGame();
    }
    }