关于提高webgame地图加载速度技术解决方案Word格式.docx
《关于提高webgame地图加载速度技术解决方案Word格式.docx》由会员分享,可在线阅读,更多相关《关于提高webgame地图加载速度技术解决方案Word格式.docx(11页珍藏版)》请在冰豆网上搜索。
mapIndexY1=uint(y1/256);
源码:
1.
2.packagecom.heptaFmon.game.map.layers
3.{
4.importcom.heptaFmon.config.Config;
5.importcom.heptaFmon.core.BaseDisplayObject;
6.importcom.heptaFmon.game.map.impl.GameMap;
7.importcom.heptaFmon.hack.HeptaFishGC;
8.importcom.heptaFmon.loader.impl.ImageLoader;
9.importcom.heptaFmon.map.impl.HashMap;
10.
11.importflash.display.Bitmap;
12.importflash.events.Event;
13.importflash.events.IOErrorEvent;
14.importflash.events.ProgressEvent;
15.importflash.geom.Point;
16.//地图层图片
17.publicclassMapLayerextendsBaseDisplayObject
18.{
19.
//图片读取器
20.
privatevar_imageLoader:
ImageLoader;
21.
//地图图片用于整块加载模式
22.
privatevar_image:
Bitmap;
23.
//地图图片数组用于栅格式加载地图模式
24.
privatevar_imageMap:
HashMap;
25.
//小地图图片
26.
privatevar_simage:
27.
//
28.
privatevar_map:
GameMap;
29.
privatevar_loadType:
int;
//加载类型0:
整块加载1:
栅格加载
30.
privatevar_visualWidth:
//地图可视宽度
31.
privatevar_visualHeight:
//地图可视高度
32.
privatevar_sliceWidth:
//地图切割单元宽度
33.
privatevar_sliceHeight:
//地图切割单元高度
34.
privatevar_preloadX:
//横向预加载屏数
35.
privatevar_preloadY:
//纵向预加载屏数
36.
privatevar_loadingMap:
//正在加载的屏map
37.
privatevar_waitLoadingArr:
Array;
//等待加载的loadermap
38.
39.
privatevar_loadingNo:
int=Config.getInt("
concurrencyImageLoader"
);
40.
41.
privatevar_screenImageRow:
//一屏需要加载的横向图片数
42.
privatevar_screenImageCol:
//一屏需要加载的纵向图片数
43.
privatevar_row:
//总横向节点数
44.
privatevar_col:
//总纵向节点数
45.
46.
privatevar_nowPlayerPointoint;
//当前人物所处的屏
47.
48.
publicfunctionMapLayer(map:
GameMap)
49.
{
50.
_map=map;
51.
_loadType=parseInt(_map.mapXML.@loadType);
52.
}
53.
//读取地图图片
54.
publicfunctionload():
void{
55.
//加载小地图
56.
varimageLoader:
ImageLoader=newImageLoader();
57.
varfileName:
String=Config.getValue("
mapLib"
)+_map.name+"
/map_s.jpg"
;
58.
imageLoader.load(fileName);
59.
imageLoader.addEventListener(Event.COMPLETE,loadSmallSuccess);
60.
imageLoader.addEventListener(ProgressEvent.PROGRESS,loadingHandler);
61.
imageLoader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
62.
63.
64.
//读取大地图成功
65.
privatefunctionloadBigSuccess(evet:
Event):
66.
ImageLoader=ImageLoader(evet.target);
67.
varimage:
Bitmap=newBitmap(imageLoader._data);
68.
addChild(image);
69.
if(_simage!
=null&
&
_simage.parent==this){
70.
removeChild(_simage);
71.
_simage=null;
72.
73.
this.width=image.width;
74.
this.height=image.height;
75.
imageLoader.removeEventListener(Event.COMPLETE,loadBigSuccess);
76.
imageLoader.removeEventListener(ProgressEvent.PROGRESS,loadingHandler);
77.
imageLoader.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
78.
imageLoader=null;
79.
dispatchEvent(evet);
80.
HeptaFishGC.gc();
81.
82.
//读取小地图成功
83.
privatefunctionloadSmallSuccess(evet:
84.
85.
86.
image.width=_map.mapWidth;
87.
image.height=_map.mapHeight;
88.
89.
90.
91.
imageLoader.removeEventListener(Event.COMPLETE,loadSmallSuccess);
92.
93.
94.
95.
96.
97.
switch(_loadType){
98.
case0:
//整块加载
99.
//加载大地图
100.
varbfileName:
/map.jpg"
101.
varbLoader:
102.
bLoader.load(bfileName);
103.
bLoader.addEventListener(Event.COMPLETE,loadBigSuccess);
104.
bLoader.addEventListener(ProgressEvent.PROGRESS,loadingHandler);
105.
bLoader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
106.
break;
107.
case1:
108.
_loadingMap=newHashMap();
109.
_imageMap=newHashMap();
110.
_waitLoadingArr=newArray();
111.
_visualWidth=_map.app.screen.size.x;
112.
_visualHeight=_map.app.screen.size.y;
113.
_sliceWidth=parseFloat(_map.mapXML.@sliceWidth);
114.
_sliceHeight=parseFloat(_map.mapXML.@sliceHeight);
115.
_preloadX=parseFloat(_map.mapXML.@preloadX);
116.
_preloadY=parseFloat(_map.mapXML.@preloadY);
117.
_screenImageRow=Math.round(_visualWidth/_sliceWidth);
118.
_screenImageCol=Math.round(_visualHeight/_sliceHeight);
119.
_row=Math.ceil(_map.mapWidth/_sliceWidth);
120.
_col=Math.ceil(_map.mapHeight/_sliceHeight);
121.
loadSliceImage(_map.initPlayerPoint);
122.
123.
default:
124.
125.
126.
127.
128.
129.
//根据player坐标读取周边指定屏数地图
130.
privatefunctionloadSliceImage(playerPointoint):
131.
varnowX:
int=Math.floor(playerPoint.x/_sliceWidth);
//现在所处的索引X
132.
varnowY:
int=Math.floor(playerPoint.y/_sliceHeight);
//现在所处的索引Y
133.
varnowScreenX:
int=Math.floor(nowX/_screenImageRow);
//现在所处的屏索引X
134.
varnowScreenY:
int=Math.floor(nowY/_screenImageCol);
//现在所处的屏索引Y
135.//
trace("
nowScreenX:
"
+nowScreenX);
136.//
nowScreenY:
+nowScreenY);
137.
_nowPlayerPoint=newPoint(nowScreenX,nowScreenY);
138.
loadScreenImage(nowScreenX,nowScreenY);
139.//
removeScreenImage(nowScreenX,nowScreenY);
140.
varstartX:
int=(nowScreenX-_preloadX<
0?
0:
nowScreenX-_preloadX);
141.
varstartY:
int=(nowScreenY-_preloadY<
nowScreenY-_preloadY);
142.
143.
varendX:
int=(nowScreenX+_preloadX>
_row?
_row:
nowScreenX+_preloadX);
144.
varendY:
int=(nowScreenY+_preloadY>
_col?
_col:
nowScreenY+_preloadY);
145.
146.
for(varxx:
int=startX;
xx<
endX;
xx++){
147.
for(varyy:
int=startY;
yy<
endY;
yy++){
148.
if(xx==nowScreenX&
yy==nowScreenY){
149.
continue;
150.
}else{
151.
loadScreenImage(xx,yy);
152.
153.
}
154.
155.
156.
//加载指定屏的地图图片
157.
privatefunctionloadScreenImage(screenX:
int,screenY:
int):
158.
varstarX:
int=_screenImageRow*screenX<
_screenImageRow*screenX;
159.
varstarY:
int=_screenImageCol*screenY<
_screenImageCol*screenY;
160.
int=_screenImageRow*(screenX+1)>
_row-1?
_row-1:
_screenImageRow*(screenX+1);
161.
int=_screenImageCol*(screenY+1)>
_col-1
?
_col-1:
_screenImageCol*(screenY+1);
162.
for(varyy:
int=starY;
yy<
endY+1;
163.
for(varxx:
int=starX;
xx<
endX+1;
164.
vartempKey:
String=yy+"
_"
+xx;
165.
if(!
_loadingMap.containsValue(tempKey)&
!
_imageMap.containsKey(tempKey)){
166.
_waitLoadingArr.push(tempKey);
167.
168.
169.
_waitLoadingArr.reverse();
170.
loadImage();
171.
172.
173.
174.
privatefunctionloadImage():
175.
if(_waitLoadingArr.length>
0){
176.
for(vari:
int=0;
i<
_loadingNo-_loadingMap.size();
i++){
177.
varkey:
String=_waitLoadingArr.pop();
178.
179.
String=Config.getValue("
)+_map.name+"
/"
+key
+"
.jpg"
180.//
fileName:
+fileName);
181.
_loadingMap.put(imageLoader,key);
182.
imageLoader.addEventListener(Event.COMPLETE,loadScreenImageSuccess);
183.
184.
185.
186.
187.
188.
189.
190.
//成功加载某屏的图片
191.
privatefunctionloadScreenImageSuccess(evet:
192.
193.
vartempStr:
String=String(_loadingMap.getValue(imageLoader));
194.
vartempStrArr:
Array=tempStr.split("
195.
varyy:
int=tempStrArr[0];
196.
varxx:
int=tempStrArr[1];
197.
_loadingMap.remove(imageLoader);
198.
199.
image.x=_sliceWidth*xx;
200.
image.y=_sliceHeight*yy;
201.
this.addChild(image);
202.
_imageMap.put(yy+"
+xx,image);
203.
imageLoader.removeEventListener(Event.COMPLETE,loadScreenImageSuccess);
204.
imageLoader.removeEventListener(ProgressEvent.PROGRESS,loadingH