Phaser 环境设置(Asp.Net MVC5 - Typescript - Visual Studio 2015)

创建一个新的 ASP.Net 项目:

StackOverflow 文档

选择空模板:

StackOverflow 文档

在根文件夹中添加两个新文件夹:AppScripts

StackOverflow 文档

在根文件夹中添加 npm 配置文件:

StackOverflow 文档

{
    "version": "1.0.0",
    "name": "phaser.js.environment.setup",
    "private": true,
      "devDependencies": {
        "gulp": "3.9.1",
        "phaser": "2.6.2"
      }
}

在根文件夹中添加 gulp 配置文件:

StackOverflow 文档

Scripts 文件添加 typings 文件夹:

StackOverflow 文档

Gulp 任务:

/// <binding ProjectOpened='install' />

var gulp = require('gulp');

gulp.task('phaser-setup-typings', function () {
    gulp.src([
        './node_modules/phaser/typescript/pixi.d.ts',
        './node_modules/phaser/typescript/p2.d.ts',
        './node_modules/phaser/typescript/phaser.d.ts',
    ])
   .pipe(gulp.dest('./Scripts/typings'));
});

gulp.task('phaser-setup', function () {
    gulp.src([
        './node_modules/phaser/build/phaser.min.js',
    ])
   .pipe(gulp.dest('./Scripts/'));
});

gulp.task('install', ['phaser-setup-typings', 'phaser-setup']);

运行安装任务:

StackOverflow 文档

App 文件夹中添加 typescript 文件:

StackOverflow 文档

添加 MVC 控制器

StackOverflow 文档

using System.Web.Mvc;

namespace PhaserSetUp.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
    }
}

添加 web optimization nuget

Install-Package Microsoft.AspNet.Web.Optimization

StackOverflow 文档

BundleConfig.cs 类添加到 App_Start 文件夹中:

using System.Web.Optimization;

namespace PhaserSetUp.App_Start
{
    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/app").Include(
                        "~/App/app.js"));
        }
    }
}

编辑 Global.asax

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Http;

namespace PhaserSetUp
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);  
            BundleConfig.RegisterBundles(BundleTable.Bundles);         
        }
    }
}

添加视图:

StackOverflow 文档

@using System.Web.Optimization
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
</head>
<body>
    <div>
        @RenderBody()
    </div>
    <script src="../../Scripts/phaser.min.js"></script>
    @Scripts.Render("~/bundles/app")
</body>
</html>