如何将OpenXLive添加到WP7 Silverlight游戏中

简介:
8"?>

<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2009/deployment" AppPlatformVersion="7.0">
  <App xmlns="" ProductID="{3cf35939-f7f6-4808-969c-22d520f6a526}" Title="OpenXLiveGameSilverlight" RuntimeType="Silverlight" Version="1.0.0.0" Genre="apps.normal"  Author="OpenXLiveGameSilverlight author" Description="Sample description" Publisher="OpenXLiveGameSilverlight">
    <IconPath IsRelative="true" IsResource="false">ApplicationIcon.png</IconPath>
    <Capabilities>
      <Capability Name="ID_CAP_GAMERSERVICES"/>
    </Capabilities>
    <Tasks>
      <DefaultTask  Name ="_default" NavigationPage="OpenXLive.Silverlight;component/Forms/StartupPage.xaml"/>
    </Tasks>
    <Tokens>
      <PrimaryToken TokenID="OpenXLiveGameSilverlightToken" TaskName="_default">
        <TemplateType5>
          <BackgroundImageURI IsRelative="true" IsResource="false">Background.png</BackgroundImageURI>
          <Count>0</Count>
          <Title>OpenXLiveGameSilverlight</Title>
        </TemplateType5>
      </PrimaryToken>
    </Tokens>
  </App>
</Deployment>

在WMAppMainifest.xml文件中找到DefaultTask节点,并将其中的NavigationPage改为”OpenXLive.Silverlight;component/Forms/StartupPage.xaml”。这样能保证Silverlight游戏启动之后,会首先启动OpenXLive的Startup页面。

接下来,我们还要完成对XLiveSLFormManager对象的初始化工作。打开工程中的App.xaml.cs文件,首先在文件顶部加入对OpenXLive.Silverlight的引用:

using OpenXLive.Silverlight;

然后,在App类中找到Application_Launching方法,加入XLiveSLFormManager对象的创建操作:

        // Code to execute when the application is launching (eg, from Start)
        // This code will not execute when the application is reactivated
        private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            XLiveSLFormManager manager = new XLiveSLFormManager(this, "xxxxxxxxxxxxxxxx");
        }

其中,第一个参数为App实例的引用,第二个参数为SecretKey,该Key是在OpenXLive网站上创建游戏时,由系统生成的,作为OpenXLive系统识别游戏的唯一标识符,请保证这个SecretKey的安全,以防止其他游戏进行仿冒。

更多详细情况,请查看《在开发者网站上创建OpenXLive游戏》,链接如下:

http://wiki.openxlive.net/Tutorial-4-Create-OpenXLive-Game-in-website.ashx

当然,我们也可以在App类中创建一个static的FormManager属性,用于在程序的其他地方调用XLiveSLFormManager对象,但这一步不是必须的,代码如下:

        public static XLiveSLFormManager FromManager { get; internal set; }

        // Code to execute when the application is launching (eg, from Start)
        // This code will not execute when the application is reactivated
        private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            FromManager = new XLiveSLFormManager(this, "xxxxxxxxxxxxxxxxxx");
        }

我们运行程序,能够看到下面的界面,是不是有一种似曾相识的感觉?点击Game Center和Leaderboards,我们可以进入相关的界面:

SLDemo3   SL2  SL3

目前OpenXLive Silverlight只支持竖屏(Portrait)显示,未来我们会加入对于横屏(Landscape)的支持。另外,除了Startup界面之外,其他OpenXLive界面都是黑色背景,这样做主要是为了节省系统资源。

添加背景和事件处理

接下来,为了美化启动界面,我们要加入背景图片。添加背景图片的操作非常简单:在Solution Explorer中,选择Project节点,右键单击,选择Add – Existing Item,在选择一副PNG或JPG的图片,尺寸最好是800x480,这样能够有效保证图片不变形。

另外请确认一下,我们加入的图片Build Action为Resource,且Copy to Output Directory选择“Do not copy”。

然后,回到App.xaml.cs文件的Application_Launching方法中:

        // Code to execute when the application is launching (eg, from Start)
        // This code will not execute when the application is reactivated
        private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            FromManager = new XLiveSLFormManager(this, "xxxxxxxxxxxxxxxxxxx");
            StartupPage.BackgroundPath = "/OpenXLiveGameSilverlight;component/OpenXLive.Portrait.png";
        }

我们要为StartupPage添加BackgroundPath,示例代码的写法是针对游戏本身的资源,也可以指定一个来自Web的链接,不过出于显示效果的考虑,建议您不要这么做。OpenXLiveGameSilverlight,是资源所在程序集的名称;component/OpenXLive.Portrait.png,是资源的引用路径,如果资源包含在一个文件夹中,写法应该是component/MyFolder/OpenXLive.Portrait.png。

最后提醒,请大家千万不要忘记字符串前面的“/”,没有这个符号,程序将抛出异常。

接下来,我们要加入Startup页面的事件处理函数,当用户点击某一个按钮时,会触发一个事件操作。我们在下面增加了其中两个按钮的事件处理方法:

        public static XLiveSLFormManager FromManager { get; internal set; }

        // Code to execute when the application is launching (eg, from Start)
        // This code will not execute when the application is reactivated
        private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            FromManager = new XLiveSLFormManager(this, "xxxxxxxxxxxxxxxxxx");
            StartupPage.BackgroundPath = "/OpenXLiveGameSilverlight;component/OpenXLive.Portrait.png";
            StartupPage.NewGameButtonClick += new EventHandler(StartupPage_NewGameButtonClick);
            StartupPage.AboutButtonClick += new EventHandler(StartupPage_AboutButtonClick);
        }

        void StartupPage_AboutButtonClick(object sender, EventArgs e)
        {
            MessageBox.Show("OpenXLive Silverlight Demo 1.0");
        }

        void StartupPage_NewGameButtonClick(object sender, EventArgs e)
        {
            this.RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

AboutButtonClick的处理函数非常简单,只是调用了Message来显示游戏的版本信息。而NewGameButtonClick则是为了调用开发人员自己编写的Silverlight游戏界面,这个界面应该存在于Silverlight游戏的程序集中。

SL1

最后,我们得到的Silverlight启动界面就是上图这样。我们在Silverlight的界面中,没有增加Exit选项,因为Silverlight界面默认不支持退出操作,只能通过按Back键来退出程序。

使用Wizard创建新游戏

接下来,我们要介绍如何使用Visual Studio 2010的Template来创建一个OpenXLive Silverlight游戏。OpenXLive Silverlight的模板会包括在OpenXLive SDK中,如果您安装了OpenXLive SDK,就可以在Visual Studio 2010中创建基于Silverlight的游戏了。

首先,打开Visual Studio 2010,在File菜单中选择New-Project,在New Project对话框中,选择Visual C#下的Silverlight for Windows Phone中的OpenXLive Silverlight节点,然后选择创建OpenXLiveSilverlightGame。

Wizard1

OpenXLiveSilverlightGame工程创建完毕后,如果直接编译,会得到下列的错误提示:

Wizard2

这个错误是为了提醒开发者,要在OpenXLive开发者网站上创建自己的游戏,获取游戏对应的Secret Key,才能够正常使用OpenXLive的在线功能。

获取Secret Key的方法,请参考《在开发者网站上创建OpenXLive游戏》,链接如下:

http://wiki.openxlive.net/Tutorial-4-Create-OpenXLive-Game-in-website.ashx

打开App.xaml.cs文件,找到下面的代码:

public static XLiveSLFormManager FromManager { get; internal set; }

#error Please full your game Secret Key in below code
private string APISecretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";

// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
    FromManager = new XLiveSLFormManager(this, APISecretKey);
    StartupPage.BackgroundPath = "/OpenXLiveSilverlightGame2;component/OpenXLive.Portrait.png";
    StartupPage.NewGameButtonClick += new EventHandler(StartupPage_NewGameButtonClick);
    StartupPage.AboutButtonClick += new EventHandler(StartupPage_AboutButtonClick);
}

开发者将Secret Key加入到高亮显示部分,并且注释掉#error代码,我们的工程就可以正常编译了。

我们也可以修改New Game Button的事件处理函数,来决定调用哪个Silverlight窗体。

void StartupPage_NewGameButtonClick(object sender, EventArgs e)
{
    this.RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}

到这里,我们就介绍完了如何通过Visual Studio 2010向导来创建OpenXLive Silverlight游戏的方法。

写在最后

我们用几乎与XNA版本相同的顺序,叙述了如何将OpenXLive Silverlight加入到Windows Phone Silverlight的游戏中去。接下来,我们会介绍,如何提交成绩、提交成就,考虑到Silverlight的界面编程更加容易,我们并不会提供一个标准的成绩提交界面,该界面只会以示例代码的形式出现。

http://wiki.openxlive.net/OpenXLive-Silverlight.ashx

引用

OpenXLive官方网站

http://www.openxlive.net/

OpenXLive开发者网站

http://developer.openxlive.net/

OpenXLive开发入门

http://wiki.openxlive.net/Getting-Started-with-Open-XLive.ashx


本文转自马宁博客园博客,原文链接:http://www.cnblogs.com/aawolf/archive/2011/04/02/2003381.html,如需转载请自行联系原作者

相关文章

热门文章

最新文章