2

我想制作一个具有 Silverlight 菜单的应用程序,但该应用程序的游戏部分是 XNA。我正在尝试使用此示例游戏中的代码和在 Silverlight 中进行 XNA 渲染的方法使 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 是如何工作的,我不得不注释掉 base.initialize、更新和绘制但是 base.OnNavigatedFrom 有效。

另外,我应该能够让我的代码在理论上工作吗?我发现它非常复杂,尽管阅读有关 XNA/Silverlight 的信息是可能的,但我找不到任何人们成功地将 XNA 和 Silverlight 组合到同一个应用程序中的源代码。

4

1 回答 1

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 Combined 平台几乎是一样的,只是点点滴滴不同,你甚至可以要求 XNA 渲染一些 SilverLight 控件,这样可以更容易地处理你游戏中的一些按钮事件。

希望这可以帮助

于 2012-11-29T06:06:07.630 回答