2012-11-28 69 views
2

我想製作一個具有Silverlight菜單的應用程序,但該應用程序的遊戲部分是XNA。我正在嘗試使用this example game中的代碼以及在Silverlight here中執行XNA呈現的方法來獲取Silverlight/XNA分割工作。結合這2個教程我的源代碼,看起來像這樣:WP7 Silverlight/XNA分割

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Input.Touch; 
using Microsoft.Xna.Framework.Media; 
using Microsoft.Phone.Controls; 
using System.Windows.Navigation; 

namespace FYP 
{ 
    public partial class GamePage : PhoneApplicationPage 
    { 
     GameTimer timer; 
     SpriteBatch spriteBatch; 
     Texture2D ballTexture; 
     IList<Ball> balls = new List<Ball>(); 
     bool touching = false; 

     public GamePage(ContentManager contentManager) 
     { 
      InitializeComponent(); 

      //base.Initialize(); 

      // Create a timer for this page 
      timer = new GameTimer(); 
      timer.UpdateInterval = TimeSpan.FromTicks(333333); 
      //timer.Update += OnUpdate; 
      //timer.Draw += OnDraw; 

      // TODO: use this.Content to load your game content here 
      ballTexture = contentManager.Load<Texture2D>("Ball"); 
     } 

     protected override void OnNavigatedTo(NavigationEventArgs e) 
     { 
      base.OnNavigatedTo(e); 

      // Set the sharing mode of the graphics device to turn on XNA rendering 
      SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true); 

      timer.Start(); 

      // Create a new SpriteBatch, which can be used to draw textures. 
      spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice); 
     } 

     protected override void OnNavigatedFrom(NavigationEventArgs e) 
     { 
      base.OnNavigatedFrom(e); 

      // Set the sharing mode of the graphics device to turn off XNA rendering 
      SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false); 

      // Stop the timer 
      timer.Stop(); 
     } 

     private void OnUpdate(GameTime gameTime) 
     { 
      // Allows the game to exit 
      if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
       //this.Exit(); 

      // TODO: Add your update logic here 

      //base.Update(gameTime); 

      HandleTouches(); 
      UpdateBalls(); 
     } 

     private void OnDraw(GameTime gameTime) 
     { 
      SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.White); 

      // TODO: Add your drawing code here 
      foreach (Ball ball in balls) 
      { 
       ball.Draw(spriteBatch); 
      } 

      //base.Draw(gameTime); 
     } 

     private void HandleTouches() 
     { 
      TouchCollection touches = TouchPanel.GetState(); 
      if (!touching && touches.Count > 0) 
      { 
       touching = true; 
       Random random = new Random(DateTime.Now.Millisecond); 
       Color ballColor = new Color(random.Next(255), random.Next(255), random.Next(255)); 
       Vector2 velocity = new Vector2((random.NextDouble() > .5 ? -1 : 1) * random.Next(9), (random.NextDouble() > .5 ? -1 : 1) * random.Next(9)) + Vector2.UnitX + Vector2.UnitY; 
       //Vector2 center = new Vector2((float)SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width/2, (float)SharedGraphicsDeviceManager.Current.GraphicsDevice.Height/2); 
       Vector2 center = new Vector2((float)SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width/2, 200); 
       float radius = 25f * (float)random.NextDouble() + 5f; 
       balls.Add(new Ball(this, ballColor, ballTexture, center, velocity, radius)); 
      } 
      else if (touches.Count == 0) 
      { 
       touching = false; 
      } 
     } 

     private void UpdateBalls() 
     { 
      foreach (Ball ball in balls) 
      { 
       ball.Update(); 
      } 
     } 
    } 
} 

我不明白怎麼基礎工程,我不得不註釋掉base.initialize,更新,然而畫base.OnNavigatedFrom工作。

此外,我應該能夠讓我的代碼在理論上工作嗎?我發現它非常複雜,雖然閱讀XNA/Silverlight是可能的,但我無法找到任何人們已經成功將XNA和Silverlight組合到同一個應用程序中的源代碼。

+0

'base'是父類實例的引用,所以'base.Initialize()'是'到一個PhoneApplicationPage.Initialize調用()'。 – Dai

+0

好吧,所以我得到base.Initialize()的代碼,父類是Microsoft.Xna.Framework.Game。我能夠在我的代碼中調用初始化嗎? – Alex

回答

1

這些影片將幫助你瞭解XNA和Silverlight/XNA結合平臺

http://channel9.msdn.com/Series/Mango-Jump-Start/Mango-Jump-Start-11a-XNA-for-Windows-Phone--Part-1

http://channel9.msdn.com/Series/Mango-Jump-Start/Mango-Jump-Start-11b-XNA-for-Windows-Phone--Part-2

理論上XNA和Silverlight XNA綜合平臺是幾乎一樣的,只是一個差異你甚至可以讓XNA渲染一些SilverLight控件,這會讓你更容易處理遊戲中的一些按鈕事件。

希望這有助於