Skip to content

Commit 39433de

Browse files
committed
update
1 parent 84dbf95 commit 39433de

7 files changed

Lines changed: 58 additions & 7 deletions

File tree

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,53 @@
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

bin/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<!--用户自定义顺序文件添加到这里-->
3939
<!--jsfile--Custom-->
4040
<script src="./require.js"></script>
41-
<script src="./js/all.js"></script>
41+
<script src="./js/game.js"></script>
4242
<script>
4343
require(['GameMain'], function (GameMain) {
4444
new GameMain.default()

bin/js/all.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

bin/js/all.js renamed to bin/js/game.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
define("laya", ["require", "exports"], function (require, exports) {
1+
define("adapter", ["require", "exports"], function (require, exports) {
22
"use strict";
33
Object.defineProperty(exports, "__esModule", { value: true });
44
exports.EventDispatcher = Laya.EventDispatcher;
55
exports.Handler = Laya.Handler;
66
exports.Animation = Laya.Animation;
77
exports.Text = Laya.Text;
88
});
9-
define("Foo", ["require", "exports", "laya"], function (require, exports, laya_1) {
9+
define("Foo", ["require", "exports", "./laya"], function (require, exports, laya_1) {
1010
"use strict";
1111
Object.defineProperty(exports, "__esModule", { value: true });
1212
var Foo = (function () {

src/Foo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Text } from './laya';
1+
import { Text } from './adapter';
22
export class Foo {
33
constructor() {
44
const hello = new Text();
File renamed without changes.

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"module": "amd",
66
"skipLibCheck": true,
77
"outDir": "./bin",
8-
"outFile": "./bin/js/all.js"
8+
"outFile": "./bin/js/game.js"
99
},
1010
"experimentalDecorators": true,
1111
"exclude": [

0 commit comments

Comments
 (0)