2012-03-03 76 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