下面我们创建一个Xbox版本
22、为Xbox创建CrossPlatformXNA FrameworkGame项目的一个副本,在项目名称上点击右键,选择“为Xbox 360创建项目的副本”,第三个项目应该就创建好了,项目名称应该为“Xbox 360 copy of CrossPlatformXNA FrameworkGame”。
23、最后一次修改构造器,为设置游戏的分辨率添加一个事件处理程序,最终的构造器看起来应该是:
public GolfGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
#if WINDOWS_PHONE
// Hides the battery and signal strength bar
graphics.IsFullScreen = true;
#else
// Code to support changing the resolution of the game for the Xbox to handle high def screens
graphics.IsFullScreen = false;
graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);
#endif
}
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
#if WINDOWS_PHONE
// Hides the battery and signal strength bar
graphics.IsFullScreen = true;
#else
// Code to support changing the resolution of the game for the Xbox to handle high def screens
graphics.IsFullScreen = false;
graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);
#endif
}
24、现在我们必须为我们注册的事件定义函数的定义。
void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
{
#if XBOX
foreach (Microsoft.Xna.Framework.Graphics.DisplayMode displayMode in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes)
{
// High def resolution support for Xbox (if available)
if (displayMode.Width == 1920 || displayMode.Width == 1280)
{
e.GraphicsDeviceInformation.PresentationParameters.BackBufferFormat = displayMode.Format;
e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = displayMode.Height;
e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth = displayMode.Width;
}
}
#endif
}
{
#if XBOX
foreach (Microsoft.Xna.Framework.Graphics.DisplayMode displayMode in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes)
{
// High def resolution support for Xbox (if available)
if (displayMode.Width == 1920 || displayMode.Width == 1280)
{
e.GraphicsDeviceInformation.PresentationParameters.BackBufferFormat = displayMode.Format;
e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = displayMode.Height;
e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth = displayMode.Width;
}
}
#endif
}
25、接下来我们必须修改Update()函数,添加Xbox控制器支持,这个函数的最终版本代码如下:
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
Vector2 velocity = Vector2.Zero;
#if WINDOWS
KeyboardState kState = Keyboard.GetState(PlayerIndex.One);
Keys[] pressedKeys = kState.GetPressedKeys();
ballVelocity = Vector2.Zero;
foreach (Keys k in pressedKeys)
switch (k)
{
case Keys.Right:
ballVelocity.X = velocityMultiplier;
break;
case Keys.Left:
ballVelocity.X = -velocityMultiplier;
break;
case Keys.Down:
ballVelocity.Y = velocityMultiplier;
break;
case Keys.Up:
ballVelocity.Y = -velocityMultiplier;
break;
}
#elif XBOX
ballVelocity.X = velocityMultiplier * GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X;
ballVelocity.Y = -velocityMultiplier * GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y;
#endif
ballPt += ballVelocity;
if (CheckForCollision())
ResetGame();
base.Update(gameTime);
}
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
Vector2 velocity = Vector2.Zero;
#if WINDOWS
KeyboardState kState = Keyboard.GetState(PlayerIndex.One);
Keys[] pressedKeys = kState.GetPressedKeys();
ballVelocity = Vector2.Zero;
foreach (Keys k in pressedKeys)
switch (k)
{
case Keys.Right:
ballVelocity.X = velocityMultiplier;
break;
case Keys.Left:
ballVelocity.X = -velocityMultiplier;
break;
case Keys.Down:
ballVelocity.Y = velocityMultiplier;
break;
case Keys.Up:
ballVelocity.Y = -velocityMultiplier;
break;
}
#elif XBOX
ballVelocity.X = velocityMultiplier * GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X;
ballVelocity.Y = -velocityMultiplier * GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y;
#endif
ballPt += ballVelocity;
if (CheckForCollision())
ResetGame();
base.Update(gameTime);
}
26、就这样,我们编写了一个可以运行在三个平台上的应用程序,是不是很简单呢?