Skip to content

Commit d833b82

Browse files
author
Guoshaorui
committed
09
1 parent c618ac1 commit d833b82

30 files changed

+14271
-315
lines changed

egret/09-resource.md

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,40 @@ Egret框架入门第一步 - 资源管理和屏幕适配
55

66
先来看资源加载方面需要注意的一些事情,游戏开发之前您需要设计一下素材的目录结构,如果存在多套不同分辨率的资源,您可以使用assets/480/,assets/640/这样的方式来设计素材目录结构加以区分,如果只有一套资源,也可以不遵守assets/480/这样的规则,将prefix指向您自己的素材目录即可。
77

8-
```
9-
egret.ResourceLoader.prefix = "assets/480/";
10-
```
11-
12-
加载的实现,推荐使用LoadingController:
13-
14-
```
15-
var loadingController = new egret.LoadingController();
16-
```
17-
18-
加载某个资源,将根据设定的ResourceLoader.prefix来获取,您不用重复传入完整路径,只需传入相对于资源根目录的路径。
19-
```
20-
loadingController.addResource("egret_icon.png", egret.ResourceLoader.DATA_TYPE_IMAGE);
8+
加载的实现,推荐使用RES模块:
9+
10+
```
11+
//使用RES模块,侦听GROUP_COMPLETE事件和GROUP_PROGRESS事件,可以同步显示加载进度,并继续执行加载完成后的逻辑
12+
RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE,this.onResourceLoadComplete,this);
13+
RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS,this.onResourceProgress,this);
14+
RES.loadConfig("resources/resource.json","resources/");//加载资源配置文件
15+
RES.loadGroup("preload");//加载某个资源group
16+
```
17+
18+
然后将您需要加载的资源,都配置在resource.json中即可,注意路径是相对RES.loadConfig中传递的第二个参数而言的。
19+
```
20+
{
21+
"resources":
22+
[
23+
{"name":"bgImage","type":"img","url":"assets/bg.jpg"},
24+
{"name":"egretIcon","type":"img","url":"assets/egret_icon.png"},
25+
{"name":"description","type":"json","url":"config/description.json"},
26+
{"name":"icons","type":"sheet","url":"assets/icons.json"},
27+
{"name":"monkey_png","type":"img","url":"assets/monkey.png"},
28+
{"name":"monkey_json","type":"json","url":"assets/monkey.json"},
29+
{"name":"bitmapFont","type":"font","url":"assets/font.fnt"}
30+
],
31+
32+
"groups":
33+
[
34+
{"name":"preload","keys":"bgImage,egretIcon"},
35+
{"name":"demo2","keys":"egretIcon,icons"},
36+
{"name":"demo3","keys":"monkey_png,monkey_json"},
37+
{"name":"demo4","keys":"bitmapFont"},
38+
{"name":"demo7","keys":"egretIcon"},
39+
{"name":"demo8","keys":"egretIcon"}
40+
]
41+
}
2142
```
2243

2344
然后来看一下如何设置屏幕适配的策略,首先,先获取所在环境的容器:
@@ -50,6 +71,8 @@ egret.StageDelegate.getInstance().setDesignSize(480, 800, policy);
5071
```
5172
class Demo9 {
5273
74+
/**加载进度界面*/
75+
private loadingView:LoadingUI;
5376
/**测试用的位图*/
5477
private logo:egret.Bitmap;
5578
@@ -69,22 +92,27 @@ class Demo9 {
6992
egret.StageDelegate.getInstance().setDesignSize(480, 800, policy);
7093
7194
//--------------------资源加载
72-
//设置素材的根目录,如果存在多套不同分辨率的资源,您可以使用assets/480/,assets/640/这样的方式来设计素材目录结构加以区分,
73-
//如果只有一套资源,也可以不遵守assets/480/这样的规则,将prefix指向您自己的素材目录即可
74-
egret.ResourceLoader.prefix = "assets/480/";
75-
var loadingController = new egret.LoadingController();
76-
//资源加载,将根据设定的ResourceLoader.prefix来获取,您不用重复传入完整路径,只需传入相对于资源根目录的路径
77-
loadingController.addResource("egret_icon.png", egret.ResourceLoader.DATA_TYPE_IMAGE);
78-
//设置加载显示
79-
loadingController.setLoadingView(new LoadingUI());
80-
loadingController.addEventListener(egret.ResourceLoader.LOAD_COMPLETE, this.onResourceLoadComplete, this);
81-
loadingController.load();
95+
//使用RES模块,侦听GROUP_COMPLETE事件和GROUP_PROGRESS事件,可以同步显示加载进度,并继续执行加载完成后的逻辑
96+
RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE,this.onResourceLoadComplete,this);
97+
RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS,this.onResourceProgress,this);
98+
//如果存在多套不同分辨率的资源,您可以使用assets/480/,assets/640/这样的方式来设计素材目录结构加以区分,
99+
RES.loadConfig("resources/resource.json","resources/");//加载资源配置文件
100+
RES.loadGroup("preload");//加载某个资源group
101+
102+
//-------------------设置加载进度界面
103+
this.loadingView = new LoadingUI();
104+
this.loadingView.addToStage();
105+
}
106+
/**preload资源组加载进度*/
107+
private onResourceProgress(event:RES.ResourceEvent):void {
108+
this.loadingView.onProgress(event.itemsLoaded,event.itemsTotal);
82109
}
83110
/**显示*/
84111
private onResourceLoadComplete():void {
112+
this.loadingView.removeFromStage();
85113
var stage = egret.MainContext.instance.stage;//获取Stage引用
86114
this.logo = new egret.Bitmap();//创建位图
87-
this.logo.texture = egret.TextureCache.getInstance().getTexture("egret_icon.png");//设置纹理
115+
this.logo.texture = RES.getRes("egretIcon");//设置纹理
88116
stage.addChild(this.logo);//添加到显示列表
89117
}
90118

egret/demo/HelloEgret/assets/480/icons.json

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

egret/demo/HelloEgret/launcher/egret_loader.js

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
/**
2-
* Created by apple on 14-2-27.
2+
* Copyright (c) 2014,Egret-Labs.org
3+
* All rights reserved.
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of the Egret-Labs.org nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY EGRET-LABS.ORG AND CONTRIBUTORS "AS IS" AND ANY
17+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL EGRET-LABS.ORG AND CONTRIBUTORS BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
326
*/
427

528
egret_h5 = {};
@@ -8,32 +31,18 @@ egret_h5.prefix = "";
831

932
egret_h5.loadScript = function (list, callback) {
1033
var loaded = 0;
11-
if (navigator.userAgent.indexOf("Trident/5") > -1) {
12-
//ie9
13-
var loadNext = function () {
14-
egret_h5.loadSingleScript(egret_h5.prefix + list[loaded], function () {
15-
loaded++;
16-
if (loaded >= list.length) {
17-
callback();
18-
}
19-
else {
20-
loadNext();
21-
}
22-
})
23-
};
24-
loadNext();
25-
}
26-
else {
27-
list.forEach(function (f, i) {
28-
29-
egret_h5.loadSingleScript(egret_h5.prefix + f, function () {
30-
loaded++;
31-
if (loaded >= list.length) {
32-
callback();
33-
}
34-
})
35-
});
36-
}
34+
var loadNext = function () {
35+
egret_h5.loadSingleScript(egret_h5.prefix + list[loaded], function () {
36+
loaded++;
37+
if (loaded >= list.length) {
38+
callback();
39+
}
40+
else {
41+
loadNext();
42+
}
43+
})
44+
};
45+
loadNext();
3746
}
3847

3948
egret_h5.loadSingleScript = function (src, callback) {
@@ -50,21 +59,17 @@ egret_h5.loadSingleScript = function (src, callback) {
5059
}
5160

5261
egret_h5.startGame = function () {
53-
var canvas = document.getElementById(ns_egret.StageDelegate.canvas_name);
54-
context = ns_egret.MainContext.instance;
55-
context.rendererContext = new ns_egret.HTML5CanvasRenderer(canvas);
56-
context.netContext = new ns_egret.HTML5NetContext();
57-
context.soundContext = new ns_egret.HTML5SoundContext();
58-
context.touchContext = new ns_egret.TouchContext(canvas);
59-
context.deviceContext = new ns_egret.HTML5DeviceContext();
60-
context.stage = new ns_egret.Stage(canvas.width,canvas.height);
61-
62-
ns_egret.ResourceLoader.prefix = "assets/480/";
63-
ns_egret.RendererContext.CONTENT_SCALE_FACTOR = 1;
64-
65-
// ns_egret.StageDelegate.getInstance().setResolutionPolicy(1);
66-
// ns_egret.StageDelegate.getInstance().setFrameSize(480,400);
67-
// ns_egret.StageDelegate.getInstance().setDesignSize(480,400,2);
62+
var canvas = document.getElementById(egret.StageDelegate.canvas_name);
63+
context = egret.MainContext.instance;
64+
context.rendererContext = new egret.HTML5CanvasRenderer(canvas);
65+
context.soundContext = new egret.HTML5SoundContext();
66+
context.touchContext = new egret.HTML5TouchContext(canvas);
67+
context.deviceContext = new egret.HTML5DeviceContext();
68+
context.netContext = new egret.HTML5NetContext();
69+
context.stage = new egret.Stage(canvas.width, canvas.height);
70+
;
71+
egret.TextureCache.getInstance().prefix = "assets/480/";
72+
egret.RendererContext.CONTENT_SCALE_FACTOR = 1;
6873
context.run();
6974

7075
if (app && app.startGame) {

egret/demo/HelloEgret/launcher/index.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<meta name="baidu-tc-cerfication" content="bb5e4ac079a479fb5ddcd76557a48ed3"/>
65
<title>HelloWorld</title>
76
<meta name="viewport"
87
content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no,target-densitydpi=device-dpi"/>
@@ -23,20 +22,23 @@
2322
margin: 0;
2423
height: 100%;
2524
}
25+
html {
26+
-ms-touch-action: none; /* Direct all pointer events to JavaScript code. */
27+
}
2628
</style>
2729
</head>
2830
<body>
2931
<div style="display:inline-block;width:100%; height:100%;margin: 0 auto; background: black; position:relative;"
3032
id="gameDiv">
3133
<canvas id="gameCanvas" width="480" height="800" style="background-color: #000000"></canvas>
3234
</div>
33-
<script src="../egret/src/egret_file_list.js"></script>
35+
<script src="bin-debug/lib/egret_file_list.js"></script>
3436
<script src="launcher/egret_loader.js"></script>
35-
<script src="src/game_file_list.js"></script>
37+
<script src="bin-debug/src/game_file_list.js"></script>
3638

3739
<script>
38-
egret_h5.preloadScript(egret_file_list, "../egret/src/");
39-
egret_h5.preloadScript(game_file_list, "src/");
40+
egret_h5.preloadScript(egret_file_list, "bin-debug/lib/");
41+
egret_h5.preloadScript(game_file_list, "bin-debug/src/");
4042
egret_h5.startLoading();
4143
</script>
4244
</body>
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)