|
1 | | -# HTML5-Game-AMD-TypeScript |
| 1 | +# 在基于TypeScript的LayaAir HTML5游戏开发中使用AMD |
| 2 | + |
| 3 | +## AMD |
| 4 | +AMD是"Asynchronous Module Definition"的缩写,意思就是"异步模块定义"。这是目前最流行的JavaScript/TypeScript代码的管理方式。本文介绍了如何在基于TypeScript的LayaAir HTML5游戏开发中使用AMD来组织代码。本文参考资料列表中包含了更多关于AMD的信息。 |
| 5 | + |
| 6 | +## 示例项目 |
| 7 | +示例项目源码从这里下载[https://github.com/wildfirecode/HTML5-Game-AMD-TypeScript][demo]. |
| 8 | + |
| 9 | +## 创建项目 |
| 10 | +依然使用LayaIDE来创建项目,因为要使用LayaAir引擎的库文件。 |
| 11 | + |
| 12 | +## 使用Visual Studio Code作为编码调试环境 |
| 13 | +为了能够对index.html有绝对的控制权,我们不能再使用LayaIDE,为了能够编译和调试项目,我们需要添加一些额外的配置。 |
| 14 | +- 复制示例项目中的`.vscode`文件夹到自己项目的根目录。 |
| 15 | +- 在index.html文件中引入所有的游戏代码编译文件 |
| 16 | + ```html |
| 17 | + <script src="./js/game.js"></script> |
| 18 | + ``` |
| 19 | +- 在index.html文件中启动游戏。 |
| 20 | + ```html |
| 21 | + <script> |
| 22 | + require(['GameMain'], function (GameMain) { |
| 23 | + new GameMain.default() |
| 24 | + }); |
| 25 | + </script> |
| 26 | + ``` |
| 27 | +- 增加LayaAir引擎适配模块`adapter.ts`,每一个LayaAir的类都要增加一个适配。比如`Laya.EventDispatcher`: |
| 28 | + ```ts |
| 29 | + export const EventDispatcher = Laya.EventDispatcher; |
| 30 | + ``` |
| 31 | + |
| 32 | +- 使用示例项目中的`tsconfig.json`覆盖原来的`tsconfig.json`文件。 |
| 33 | + |
| 34 | +## 编译和调试 |
| 35 | +编译使用快捷键`Ctrl+Shift+B`,调试使用`F5`,过程基本和LayaAir相同。更多信息参考VSCode官方文档。 |
| 36 | + |
| 37 | +## requirejs |
| 38 | +从requirejs官方下载库文件,目前的最新版本是2.3.4,这是下载链接[http://requirejs.org/docs/release/2.3.4/minified/require.js][lib]。把下载的库文件放入`./bin`目录,并且在index.html增加文件引用,具体参见示例项目。 |
| 39 | + |
| 40 | +## 插件推荐 |
| 41 | +为了能够自动import,需要下载额外的VSCode插件,这里是插件地址 [https://marketplace.visualstudio.com/items?itemName=rbbit.typescript-hero][autoimport]。 |
| 42 | + |
| 43 | +## 参考 |
| 44 | +- [Javascript模块化编程(1)][js1] |
| 45 | +- [Javascript模块化编程(2)][js2] |
| 46 | +- [Javascript模块化编程(3)][js3] |
| 47 | + |
| 48 | +[js1]: http://www.ruanyifeng.com/blog/2012/10/javascript_module.html |
| 49 | +[js2]: http://www.ruanyifeng.com/blog/2012/10/asynchronous_module_definition.html |
| 50 | +[js3]: http://www.ruanyifeng.com/blog/2012/11/require_js.html |
| 51 | +[lib]: http://requirejs.org/docs/release/2.3.4/minified/require.js |
| 52 | +[demo]: https://github.com/wildfirecode/HTML5-Game-AMD-TypeScript |
| 53 | +[autoimport]: https://marketplace.visualstudio.com/items?itemName=rbbit.typescript-hero |
0 commit comments