日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

分块加载(转)

發(fā)布時(shí)間:2023/12/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 分块加载(转) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

.地圖切割命名,把一張地圖切割成200*200大小小圖,命名0_0.gif這樣的格式。這個(gè)簡單不解釋。

2.分塊分析,這里運(yùn)用的就是數(shù)組,假如一張大小6*6地圖,初始化加載0_0–>3_3,這樣16張,而我們只顯示1_1–>2_2這么四張,當(dāng)顯示2_2—>3_3這樣的四張的時(shí)候,就要移除0_0,0_1,0_2,0_3,1_0,2_0,3_0.這樣的幾張,添加4_0,4_1,4_2,4_3,1_4,2_4,3_4.

3.然后把一開始加載的放在一個(gè)數(shù)組里邊,需要加載的放在另一個(gè)里邊,對比一下,需要加載4_0,4_1,4_2,4_3,1_4,2_4,3_4.而沒有加載的加載他,移除0_0,0_1,0_2,0_3,1_0,2_0,3_0。然后齊活。原理就是這個(gè)樣子了。

主函數(shù),很水的點(diǎn)擊移動(dòng)效果:

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="400" initialize="init()">

??? <mx:Script>

??????? <![CDATA[

??????????? import com.wind.Maplayer;

??????????? private var mpl:Maplayer;

??????????? private var vx:Number;

??????????? private var vy:Number;

??????????? private var sx:Number;

??????????? private var sy:Number;

??????????? private function init():void{

??????????????? vx = 500;

??????????????? vy = 500;

??????????????? sx = Math.floor(vx/200);

??????????????? sy = Math.floor(vx/200);

??????????????? mpl = new Maplayer();

??????????????? mpl.load(vx,vy);

??????????????? mpl.x = 200-vx;

??????????????? mpl.y = 200-vy;

??????????????? uic.addChild(mpl);

??????????????? addEventListener(Event.ADDED_TO_STAGE,AddedToStageHandler);

??????????? }

??????????? protected function AddedToStageHandler(e:Event):void{

??????????????? this.stage.addEventListener(MouseEvent.CLICK,button1_clickHandler);

??????????????? removeEventListener(Event.ADDED_TO_STAGE,AddedToStageHandler);

??????????? }

??????????? protected function button1_clickHandler(event:MouseEvent):void

??????????? {

??????????????? //trace (mouseX+"=======>"+mouseY);

??????????????? var moux:int = mouseX-200;

??????????????? var mouy:int = mouseY-200;

??????????????? vx +=20;

??????????????? vy +=20;

??????????????? mpl.x = 200-vx;

??????????????? mpl.y = 200-vy;

??????????????? if (!(Math.floor(vx/200)==sx)&&(Math.floor(vy/200)-sy)){

??????????????????? sx=Math.floor(vx/200);

??????????????????? sy=Math.floor(vy/200);

??????????????????? mpl.load(vx,vy);

??????????????????? mpl.x = 200-vx;

??????????????????? mpl.y = 200-vy;

??????????????? }

??????????? }

??????? ]]>

??? </mx:Script>

??? <mx:UIComponent id="uic"/>

</mx:Application>

//修改自網(wǎng)上某些類,可能有些沒必要參數(shù)及函數(shù)及類包。- -!~

package com.wind{

??? import com.wind.BaseDisplayObject;

??? import flash.display.Bitmap;

??? import flash.events.Event;

??? import flash.events.IOErrorEvent;

??? import flash.events.ProgressEvent;

??? import flash.geom.Point;

??? //地圖層 圖片

??? public class Maplayer extends BaseDisplayObject {

??????? //圖片讀取器

??????? private var _imageLoader:ImageLoader;

??????? //地圖圖片 用于整塊加載模式

??????? private var _image:Bitmap;

??????? //小地圖圖片

??????? private var _simage:Bitmap;

??????? private var _loadType:int;//加載類型 0:整塊加載 1:柵格加載

??????? private var _visualWidth:Number;//地圖可視寬度

??????? private var _visualHeight:Number;//地圖可視高度

??????? private var _sliceWidth:Number;//地圖切割單元寬度

??????? private var _sliceHeight:Number;//地圖切割單元高度

??????? private var _preloadX:Number;//橫向預(yù)加載屏數(shù)

??????? private var _preloadY:Number;//縱向預(yù)加載屏數(shù)

??????? private var _loadingMap:HashMap;//正在加載的屏map

??????? private var _imageMap:HashMap;//存放緩存地圖

??????? private var _screenImageRow:int;//一屏需要加載的橫向圖片數(shù)

??????? private var _screenImageCol:int;//一屏需要加載的縱向圖片數(shù)

??????? private var _row:int;//總橫向節(jié)點(diǎn)數(shù)

??????? private var _col:int;//總縱向節(jié)點(diǎn)數(shù)

??????? private var _waitLoadingArr:Array = new Array();

??????? public function Maplayer() {

??????????? _loadingMap = new HashMap();

??????????? _imageMap = new HashMap();

??????? }

??????? public function load(vx:int,vy:int):void {

??????????? _preloadX = 2;

??????????? _preloadY = 2;

??????????? _sliceWidth = 200;//地圖切割單元寬度

??????????? _sliceHeight = 200;

??????????? _row = 18;//總橫向節(jié)點(diǎn)數(shù)

??????????? _col = 8;//總縱向節(jié)點(diǎn)數(shù)

??????????? _screenImageRow = 2;

??????????? _screenImageCol = 2;

??????????? var _point:Point = new Point(vx,vy);

??????????? loadSliceImage(_point);

??????????? removeScreenImage(_point)

??????? }

??????? //根據(jù)player坐標(biāo)讀取周邊指定屏數(shù)地圖

??????? private function loadSliceImage(playerPoint:Point):void {

??????????? //獲取人物所在塊坐標(biāo)

??????????? var nowScreenX:int=Math.floor(playerPoint.x/_sliceWidth);

??????????? var nowScreenY:int=Math.floor(playerPoint.y/_sliceHeight);

??????????? //計(jì)算顯示范圍坐標(biāo)

??????????? var startX:int = (nowScreenX - _preloadX < 0 ? 0 : nowScreenX - _preloadX);

??????????? var startY:int = (nowScreenY - _preloadY < 0 ? 0 : nowScreenY - _preloadY);

??????????? var endX:int = (nowScreenX + _preloadX > _row ? _row : nowScreenX + _preloadX);

??????????? var endY:int = (nowScreenY + _preloadY > _col ? _col : nowScreenY + _preloadY);

??????????? //循環(huán)加載圖片

??????????? for (var xx:int = startX; xx < endX; xx++) {

??????????????? for (var yy:int = startY; yy < endY; yy++) {

??????????????????? var tempKey:String=xx+"_"+yy;

??????????????????? if(!_loadingMap.containsValue(tempKey) && !_imageMap.containsKey(tempKey)){

??????????????????????? _waitLoadingArr.push(tempKey);

??????????????????? }

??????????????? }

??????????? }

??????????? _waitLoadingArr.reverse();

??????????? //加載圖片

??????????? loadImage();

??????? }

??????? private function loadImage():void {

??????????? //加載圖片類

??????????? if (_waitLoadingArr.length>0) {

??????????????? var arrlen:int = _waitLoadingArr.length;

??????????????? for (var i:int = 0; i<arrlen; i++) {

??????????????????? var key:String=_waitLoadingArr.pop();

??????????????????? var imageLoader:ImageLoader = new ImageLoader();

??????????????????? var fileName:String="bk/images/"+key+".gif";

??????????????????? trace("fileName:" + fileName);

??????????????????? _loadingMap.put(imageLoader,key);

??????????????????? imageLoader.addEventListener(Event.COMPLETE,loadScreenImageSuccess);

??????????????????? imageLoader.addEventListener(ProgressEvent.PROGRESS,loadingHandler);

??????????????????? imageLoader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);

??????????????????? imageLoader.load(fileName);

??????????????? }

??????????? }

??????? }

??????? //成功加載某屏的圖片

??????? private function loadScreenImageSuccess(evet:Event):void {

??????????? //加載圖片成功處理

??????????? var imageLoader:ImageLoader=ImageLoader(evet.target);

??????????? //求圖片坐標(biāo)

??????????? var tempStr:String=String(_loadingMap.getValue(imageLoader));

??????????? var tempStrArr:Array=tempStr.split("_");

??????????? var yy:int=tempStrArr[0];

??????????? var xx:int=tempStrArr[1];

??????????? trace (xx+"------->"+yy);

??????????? _loadingMap.remove(imageLoader);

??????????? //加載圖片

??????????? var image:Bitmap=new Bitmap(imageLoader._data);

??????????? image.x=_sliceWidth*xx;

??????????? image.y=_sliceHeight*yy;

??????????? this.addChild(image);

??????????? //存放圖片

??????????? _imageMap.put(yy+"_"+xx,image);

??????????? imageLoader.removeEventListener(Event.COMPLETE,loadScreenImageSuccess);

??????????? imageLoader.removeEventListener(ProgressEvent.PROGRESS,loadingHandler);

??????????? imageLoader.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);

??????????? imageLoader=null;

??????????? //loadImage();

??????? }

??????? //卸載指定屏的地圖圖片

??????? private function removeScreenImage(playerPoint:Point):void {

??????????? var nowScreenX:int=Math.floor(playerPoint.x/_sliceWidth);

??????????? var nowScreenY:int=Math.floor(playerPoint.y/_sliceHeight);

??????????? var startX:int = (nowScreenX - _preloadX < 0 ? 0 : nowScreenX - _preloadX);

??????????? var startY:int = (nowScreenY - _preloadY < 0 ? 0 : nowScreenY - _preloadY);

??????????? var endX:int = (nowScreenX + _preloadX > _row ? _row : nowScreenX + _preloadX);

??????????? var endY:int = (nowScreenY + _preloadY > _col ? _col : nowScreenY + _preloadY);

??????????? ///獲取圖片數(shù)組

??????????? var keyArr:Array=_imageMap.keys();

??????????? for (var i:int = 0; i < keyArr.length; i++) {

??????????????? //刪掉超出范圍不需要顯示的圖片

??????????????? var key:String=keyArr[i];

??????????????? var tempStrArr:Array=key.split("_");

??????????????? var yy:int=tempStrArr[0];

??????????????? var xx:int=tempStrArr[1];

??????????????? if (xx<startX||xx>endX||yy<startY||yy>endY) {

??????????????????? var image:Bitmap=Bitmap(_imageMap.getValue(key));

??????????????????? this.removeChild(image);

??????????????????? image=null;

??????????????????? _imageMap.remove(key);

??????????????? }

??????????? }

??????????? //強(qiáng)制垃圾回收

??????????? HeptaFishGC.gc();

??????? }

??? }

}

HashMap

package com.wind

{

??? import flash.utils.Dictionary;

??? public class HashMap

??? {

??????? private var _keys:Array=null;

??????? private var props:Dictionary=null;

??????? public function HashMap() {

??????????? this.clear();

??????? }

??????? public function clear():void {

??????????? this.props=new Dictionary? ;

??????????? this._keys=new Array? ;

??????? }

??????? public function containsKey(key:Object):Boolean {

??????????? return this.props[key]!=null;

??????? }

??????? public function containsValue(value:Object):Boolean {

??????????? var result:Boolean=false;

??????????? var len:uint=this.size();

??????????? if (len>0) {

??????????????? for (var i:uint=0; i<len; i++) {

??????????????????? if (this.props[this._keys[i]]==value) {

??????????????????????? result =? true;

??????????????????????? break;

??????????????????? }

??????????????? }

??????????? }

??????????? return result;

??????? }

??????? public function getValue(key:Object):Object {

??????????? return this.props[key];

??????? }

??????? public function put(key:Object,value:Object):Object {

??????????? var result:Object=null;

??????????? if (this.containsKey(key)) {

??????????????? result=this.getValue(key);

??????????????? this.props[key]=value;

??????????? } else {

??????????????? this.props[key]=value;

??????????????? this._keys.push(key);

??????????? }

??????????? return result;

??????? }

??????? public function remove(key:Object):Object {

??????????? var result:Object=null;

??????????? if (this.containsKey(key)) {

??????????????? delete this.props[key];

??????????????? var index:int=this._keys.indexOf(key);

??????????????? if (index>-1) {

??????????????????? this._keys.splice(index,1);

??????????????? }

??????????? }

??????????? return result;

??????? }

??????? public function putAll(map:HashMap):void {

??????????? this.clear();

??????????? var len:uint=map.size();

??????????? if (len>0) {

??????????????? var arr:Array=map.keys();

??????????????? for (var i:uint=0; i<len; i++) {

??????????????????? this.put(arr[i],map.getValue(arr[i]));

??????????????? }

??????????? }

??????? }

??????? public function size():uint {

??????????? return this._keys.length;

??????? }

??????? public function isEmpty():Boolean {

??????????? return this.size()? <1;

??????? }

??????? public function values():Array {

??????????? var result:Array=new Array? ;

??????????? var len:uint=this.size();

??????????? if (len>0) {

??????????????? for (var i:uint=0; i<len; i++) {

??????????????????? result.push(this.props[this._keys[i]]);

??????????????? }

??????????? }

??????????? return result;

??????? }

??????? public function keys():Array {

??????????? return this._keys;

??????? }

??? }

}

ImageLoader

package com.wind

{

??? import flash.display.BitmapData;

??? import flash.events.Event;

??? import flash.system.LoaderContext;

??? import flash.utils.ByteArray;

??? public class ImageLoader extends BaseLoader{

??????? public var _data:BitmapData;

??????? //構(gòu)造函數(shù)

??????? public function ImageLoader(obj:Object = null,lc:LoaderContext = null) {

??????????? //_type = HeptaFishConstant.TYPE_LOADER_IMAGELOADER;

??????????? if(obj != null){

??????????????? if(obj is ByteArray){

??????????????????? loadBytes(obj as ByteArray,lc);

??????????????? }else if(obj is String){

??????????????????? load(obj as String,lc);

??????????????? }else{

??????????????????? throw new Error("參數(shù)錯(cuò)誤,構(gòu)造函數(shù)第一參數(shù)只接受ByteArray或String");

??????????????? }

??????????? }

??????? }

??????? //加載成功,發(fā)布成功事件

??????? override protected function completeFun(e:Event):void {

??????????? _data = _loader.content["bitmapData"];

??????????? super.completeFun(e);

??????? }

??????? //清除

??????? override public function clear():void{

??????????? _data = null;

??????????? super.clear();

??????? }

??? }

}

HeptaFishGC

package com.wind

{

??? import flash.net.LocalConnection;

??? public class HeptaFishGC

??? {

??????? public function HeptaFishGC()

??????? {

??????? }

??????? public static function gc():void{

??????????? try{

??????????????? new LocalConnection().connect("foo");

??????????????? new LocalConnection().connect("foo");

??????????? }catch(e:Error){

??????????? }

??????? }

??? }

}

BaseLoader

package com.wind

{

??? import flash.display.Loader;

??? import flash.events.Event;

??? import flash.events.EventDispatcher;

??? import flash.events.IOErrorEvent;

??? import flash.events.ProgressEvent;

??? import flash.net.URLRequest;

??? import flash.system.LoaderContext;

??? import flash.utils.ByteArray;

??? public class BaseLoader extends EventDispatcher

??? {

??????? protected var _url:String;

??????? protected var _loader:Loader;

??????? protected var _name:String;

??????? protected var _type:String;

??????? public function BaseLoader()

??????? {

??????? }

??????? //加載

??????? public function load(url:String,lc:LoaderContext = null):void{

??????????? _url = url;

//??????????? trace(_url);

??????????? _loader = new Loader();

??????????? _loader.load(new URLRequest(url),lc);

??????????? addEvent();

??????? }

??????? //加載字節(jié)

??????? public function loadBytes(bytes:ByteArray,lc:LoaderContext = null):void{

??????????? _loader = new Loader;

??????????? _loader.loadBytes(bytes,lc);

??????????? addEvent();

??????? }

??????? //開始偵聽

??????? protected function addEvent():void{

??????????? _loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressFun);

??????????? _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeFun);

??????????? _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorFun);

??????? }

??????? //結(jié)束偵聽

??????? protected function delEvent():void{

??????????? _loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,progressFun);

??????????? _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeFun);

??????????? _loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorFun);

??????? }

???????? //加載成功,發(fā)布成功事件

??????? protected function completeFun(e:Event):void {

??????????? dispatchEvent(e);

??????????? delEvent();

??????? }

??????? //加載過程

??????? protected function progressFun(e:ProgressEvent):void{

??????????? dispatchEvent(e);

??????? }

??????? //錯(cuò)誤處理

??????? protected function ioErrorFun(e:Event):void{

??????????? trace("讀取文件出錯(cuò),未找到文件!");

??????????? //Alert.show("讀取文件出錯(cuò),未找到文件!");

??????? }

??????? //清除

??????? public function clear():void{

??????????? _loader.unload();

??????????? _loader = null;

??????????? HeptaFishGC.gc();

??????? }

??????? public function get url():String{

??????????? return _url;

??????? }

??????? public function set url(url:String):void{

??????????? _url = url;

??????? }

??????? public function get name():String{

??????????? return _name;

??????? }

??????? public function set name(name:String):void{

???????????? _name = name;

??????? }

??????? public function get loader():Loader{

??????????? return _loader;

??????? }

??????? public function get type():String{

??????????? return _type;

??????? }

??? }

}

BaseDisplayObject

package com.wind

{

??? import flash.display.DisplayObject;

??? import flash.display.DisplayObjectContainer;

??? import flash.display.Sprite;

??? import flash.events.Event;

??? import flash.events.IOErrorEvent;

??? import flash.events.ProgressEvent;

??? import flash.geom.Rectangle;

??? public class BaseDisplayObject extends Sprite

??? {

??????? public function BaseDisplayObject()

??????? {

??????????? super();

??????????? addEventListener(Event.REMOVED,removedFun);

??????? }

??????? override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0.0, useWeakReference:Boolean=false):void

??????? {

??????????? super.addEventListener(type, listener, useCapture, priority, useWeakReference);

??????? }

??????? public function get measuredMinWidth():Number

??????? {

??????????? return 0;

??????? }

??????? override public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void

??????? {

??????????? super.removeEventListener(type, listener, useCapture);

??????? }

??????? override public function dispatchEvent(event:Event):Boolean

??????? {

??????????? return super.dispatchEvent(event);

??????? }

??????? public function initialize():void

??????? {

??????? }

??????? protected function removedFun(evet:Event):void{

??????????? HeptaFishGC.gc();

??????? }

??????? protected function dispatch(eventClass:Class,eventType:String,par:Object = null):void{

??????????? var event:Event = new eventClass(eventType,par);

??????????? this.dispatchEvent(event);

??????? }

??????? protected function loadingHandler(evet:ProgressEvent):void{

??????????? dispatchEvent(evet);

??????? }

??????? protected function ioErrorHandler(evet:IOErrorEvent):void{

??????????? dispatchEvent(evet);

??????? }

??? }

}

轉(zhuǎn)載于:https://www.cnblogs.com/axyz/archive/2011/11/25/2263231.html

總結(jié)

以上是生活随笔為你收集整理的分块加载(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。