WP7 Silverlight/XNA拆分
我想制作一个有Silverlight菜单的应用程序,但该应用程序的游戏部分是XNA。我正在尝试使用Silverlight中的代码和在Silverlight中进行XNA渲染的方法来实现Silverlight/XNA拆分。结合这两个教程,我得到的源代码如下所示:WP7 Silverlight/XNA拆分,silverlight,windows-phone-7,xna,Silverlight,Windows Phone 7,Xna,我想制作一个有Silverlight菜单的应用程序,但该应用程序的游戏部分是XNA。我正在尝试使用Silverlight中的代码和在Silverlight中进行XNA渲染的方法来实现Silverlight/XNA拆分。结合这两个教程,我得到的源代码如下所示: using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Fr
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();
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Input.Touch;
使用Microsoft.Xna.Framework.Media;
使用Microsoft.Phone.Controls;
使用System.Windows.Navigation;
名称空间FYP
{
公共部分类游戏页面:PhoneApplicationPage
{
游戏计时器;
SpriteBatch SpriteBatch;
纹理2d球状纹理;
IList balls=新列表();
布尔触摸=假;
公共游戏页面(ContentManager ContentManager)
{
初始化组件();
//base.Initialize();
//为此页创建计时器
计时器=新游戏计时器();
timer.UpdateInterval=TimeSpan.FromTicks(333333);
//timer.Update+=OnUpdate;
//timer.Draw+=OnDraw;
//TODO:使用此.Content在此处加载游戏内容
ballTexture=contentManager.Load(“球”);
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
基地。导航到(e);
//将图形设备的共享模式设置为启用XNA渲染
SharedGraphicsDeviceManager.Current.GraphicsDevice.SetShareingMode(true);
timer.Start();
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);
}
受保护的覆盖无效OnNavigatedFrom(NavigationEventArgs e)
{
基于(e)的导航;
//将图形设备的共享模式设置为关闭XNA渲染
SharedGraphicsDeviceManager.Current.GraphicsDevice.SetShareingMode(false);
//停止计时
timer.Stop();
}
私有void OnUpdate(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
//这是Exit();
//TODO:在此处添加更新逻辑
//更新(游戏时间);
手触();
UpdateBalls();
}
私有void OnDraw(游戏时间游戏时间)
{
SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.White);
//TODO:在此处添加图形代码
foreach(球中球)
{
球。抽签(spriteBatch);
}
//基础。抽签(游戏时间);
}
私人真空手触()
{
TouchCollection touchs=TouchPanel.GetState();
如果(!touching&&touching.Count>0)
{
触摸=真实;
Random Random=新随机数(DateTime.Now.毫秒);
Color ballColor=新颜色(random.Next(255),random.Next(255),random.Next(255));
向量2速度=新向量2((random.NextDouble()>.5?-1:1)*random.Next(9),(random.NextDouble()>.5?-1:1)*random.Next(9))+Vector2.UnitX+Vector2.UnitY;
//Vector2中心=新矢量2((浮点)SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width/2,(浮点)SharedGraphicsDeviceManager.Current.GraphicsDevice.Height/2);
Vector2中心=新Vector2((浮点)SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width/2200);
浮动半径=25f*(浮动)随机。下一步加倍()+5f;
添加(新球(这个,球的颜色,球的纹理,中心,速度,半径));
}
else if(touchs.Count==0)
{
触摸=假;
}
}
私有void UpdateBalls()
{
foreach(球中球)
{
更新();
}
}
}
}
我不明白base是如何工作的,我不得不注释掉base.initialize、update和draw-however-base.OnNavigatedFrom-works
另外,我应该能够让我的代码在理论上工作吗?我发现它非常复杂,虽然阅读了有关XNA/Silverlight的文章,但我找不到任何源代码可以让人们成功地将XNA和Silverlight组合到同一个应用程序中。这些视频将帮助您了解XNA和Silverlight/XNA组合平台 理论上,XNA和SilverLight XNA组合平台几乎相同,只是零碎的不同,你甚至可以要求XNA渲染一些SilverLight控件,这将使你更容易处理游戏中的一些按钮事件
希望这有帮助
base
是对父类实例的引用,因此base.Initialize()
是对PhoneApplicationPage.Initialize()
的调用。好的,我从中获得base.Initialize()的代码,父类是Microsoft.Xna.Framework.Game。我能在代码中调用initialize吗?