2012-03-03 263 views
0

我想要做的是从XNA GamePage.xaml重定向到Silverlight中的其他网页。如何从XNA页面重定向到Silverlight中的页面?

例如,一旦玩家有没有更多的生命,我想通过显示与文字游戏Silverlight页面。我怎样才能做到这一点?我在onUpdate方法中尝试了这样的事情:

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

但这总是给我一个错误。应该怎么做才能工作?

感谢提前:)

回答

2

这是正确的方法!

创建一个类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); 
} 
} 

实施GameOverScreen类

现在,我们有我们需要将代码添加到Game1.cs来实现它GameOverScreen类。

首先,我们需要为新屏幕的变量。在Game1类顶部添加一个新的

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

接下来,我们需要的情况下,在Game1.Update()方法添加到switch语句的GameOverScreen:

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

我们必须为绘制做同样的()方法:

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

现在,我们需要以添加将关闭GamePlayScreen并打开GameOverScreen残局()方法。这将在满足游戏结束条件时被调用。

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

gamePlayScreen = null; 
} 

一个微小的变化需要在StartGame()方法来进行为好。在GameOverScreen我们将会给用户重新启动游戏,这将调用StartGame()方法的选项。因此,在StartGame()方法结束时,我们只需添加一行代码即可将gameOverScreen设置为null。

gameOverScreen = null; 

游戏结束条件

我们需要做的最后一件事就是以游戏结束的条件,这将在GamePlayScreen类进行处理的照顾。打开GamePlayScreen.cs。 我们在这里首先需要的是一个新的整数来保存生命的玩家,将其添加到类的顶部量:例如:

int lives = 3; 

此值并不一定是3,你可以把它改成任何你喜欢的东西。 接下来,我们需要添加代码来减少每次一片蛋糕从屏幕底部移出并移除时的生命值。当生命等于0时,Game1.EndGame()将被调用。该代码将被添加到HandleFallingCake()方法中。

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

我不认为你可以使用“导航”方法进入“游戏”页面......这不是正确的......要离开游戏使用,例如:

protected override void Update(GameTime gameTime) 

{ 

// Allows the game to exit 

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 

this.Exit(); //this 

// TODO: Add your update logic here 

base.Update(gameTime); 

} 
+0

this.Exit(); Fire onNavigatedFromMethod?因为从只有游戏页面而不是整个应用程序出来才是重要的。 – harry180 2012-03-12 20:55:30

+0

检查我的新答案 – Razor 2012-03-13 08:32:24