用位图填充的方式取续列图进行划分, 也是一种位图切割,我们可以通过该原理来对单张图片进行角色动作化处理 , 不过有时候使用scrollRect 性能更佳。

 

AS3代码
  1. package    
  2. {   
  3.     import flash.display.*;   
  4.     import flash.events.*;   
  5.     import flash.net.*;   
  6.     import flash.events.IOErrorEvent;   
  7.     import flash.system.System;   
  8.     import flash.geom.Rectangle;   
  9.     import flash.geom.Point;   
  10.     /**  
  11.      * ...  
  12.      * @lbynet   
  13.      */  
  14.     public class QBitmapData   
  15.     {   
  16.            
  17.         private var bufferContainer:DisplayObjectContainer;   
  18.         private var imagePath:String;   
  19.         private var bitmapArray:Array;   
  20.         private var _loader:Loader;   
  21.         private var size:Number;   
  22.         private var rowNum:uint;   
  23.         private var listNum:uint;   
  24.         private var count:uint;   
  25.         public var callBack:Function;   
  26.         public function QBitmapData(imagePath:String, size:Number=16,_bufferContainer:DisplayObjectContainer = null):void {   
  27.                
  28.             bufferContainer = _bufferContainer;   
  29.             this.ImagePath = imagePath;   
  30.             this.size = size;   
  31.         }   
  32.         public function set ImagePath(_path:String):void {   
  33.                
  34.             var urlRequest:URLRequest = new URLRequest(_path);   
  35.             this._loader = new Loader();   
  36.             //this._loader.contentLoaderInfo.addEventListener(Event.OPEN, openHandler);   
  37.             //this._loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);   
  38.             this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);   
  39.             //this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);   
  40.             this._loader.load(urlRequest);   
  41.         }   
  42.            
  43.            
  44.         private function completeHandler(event:Event):void {   
  45.                
  46.             var target:DisplayObject = _loader.content as DisplayObject;   
  47.                
  48.             var xpos:Number = -this.size;   
  49.             var ypos:Number = 0;   
  50.             var bmd:BitmapData = new BitmapData(target.width, target.heighttrue0);   
  51.             bmd.draw(target);   
  52.             this._loader.unload();   
  53.             this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);   
  54.                
  55.             this.rowNum = Math.floor(bmd.width / this.size);   
  56.             this.listNum = Math.floor(bmd.height / this.size);   
  57.             this.count = this.rowNum * this.listNum;   
  58.             //创建长度数组;   
  59.             bitmapArray = new Array(count);   
  60.             //   
  61.             var rec:Rectangle = new Rectangle(00this.size, this.size);   
  62.             var p:Point = new Point(00);   
  63.             for (var i:Number = 0; i < count; i++) {   
  64.                 var localBMD:BitmapData = new BitmapData(this.size, this.size);   
  65.                 if (xpos == (bmd.width - this.size)) { //换行读下一张,像是一个二维数组   
  66.                     ypos += this.size;   
  67.                     xpos = -this.size;   
  68.                 }   
  69.                 xpos += this.size;   
  70.                 rec.x = xpos;   
  71.                 rec.y = ypos;   
  72.                 localBMD.copyPixels(bmd, rec, p);   
  73.                 this.bitmapArray[i] = localBMD;   
  74.             }   
  75.                
  76.                
  77.             bmd.dispose();//释放bmd资源   
  78.                
  79.             this.callBack(this.bitmapArray,this.Index(0));   
  80.         }   
  81.            
  82.         public function Index(index:Number):BitmapData {   
  83.             if (this.bitmapArray.length<=index) {   
  84.                 throw new Error("超过索引");   
  85.             }   
  86.             return this.bitmapArray[index];   
  87.         }   
  88.            
  89.         public function get BitmapArray():Array {   
  90.             return this.bitmapArray;   
  91.         }   
  92.            
  93.         public function get RowNum():uint {   
  94.             return this.rowNum;   
  95.         }   
  96.         public function get ListNum():uint {   
  97.             return this.listNum;   
  98.         }   
  99.         public function get Count():uint {   
  100.             return this.count;   
  101.         }   
  102.         public function toString() {   
  103.             return "lbynet bitamp array";   
  104.         }   
  105.            
  106.            
  107.     }   
  108.        
  109. }  

 

AS3代码
  1. package    
  2. {   
  3.     import flash.display.*;   
  4.     import flash.events.*;   
  5.     import flash.net.*;   
  6.     import flash.events.IOErrorEvent;   
  7.     import flash.system.System;   
  8.     import flash.geom.Rectangle;   
  9.     import flash.geom.Point;   
  10.     /**  
  11.      * ...  
  12.      * @lbynet   
  13.      */  
  14.     public class QScrollRect   
  15.     {   
  16.            
  17.         private var bufferContainer:DisplayObjectContainer;   
  18.         private var imagePath:String;   
  19.         private var rectArray:Array;   
  20.         private var _loader:Loader;   
  21.         private var size:Number;   
  22.         private var rowNum:uint;   
  23.         private var listNum:uint;   
  24.         private var count:uint;   
  25.         public var callBack:Function;   
  26.         public function QScrollRect(imagePath:String, size:Number=16,_bufferContainer:DisplayObjectContainer = null):void {   
  27.                
  28.             bufferContainer = _bufferContainer;   
  29.             this.ImagePath = imagePath;   
  30.             this.size = size;   
  31.         }   
  32.         public function set ImagePath(_path:String):void {   
  33.                
  34.             var urlRequest:URLRequest = new URLRequest(_path);   
  35.             this._loader = new Loader();   
  36.             this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);   
  37.             this._loader.load(urlRequest);   
  38.         }   
  39.            
  40.            
  41.         private function completeHandler(event:Event):void {   
  42.                
  43.             var target:DisplayObject = _loader.content as DisplayObject;   
  44.                
  45.                
  46.             var xpos:Number = -this.size;   
  47.             var ypos:Number = 0;   
  48.             var bmd:BitmapData = new BitmapData(target.width, target.heighttrue0);   
  49.             bmd.draw(target);   
  50.             this._loader.unload();   
  51.             this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);   
  52.                
  53.             this.rowNum = Math.floor(bmd.width / this.size);   
  54.             this.listNum = Math.floor(bmd.height / this.size);   
  55.             this.count = this.rowNum * this.listNum;   
  56.             //创建长度数组;   
  57.             this.rectArray = new Array(count);   
  58.             var rec:Rectangle;   
  59.             var p:Point = new Point(00);   
  60.             for (var i:Number = 0; i < count; i++) {   
  61.                 rec = new Rectangle(00this.size, this.size);   
  62.                 if (xpos == (bmd.width - this.size)) { //换行读下一张,像是一个二维数组   
  63.                     ypos += this.size;   
  64.                     xpos = -this.size;   
  65.                 }   
  66.                 xpos += this.size;   
  67.                 rec.x = xpos;   
  68.                 rec.y = ypos;   
  69.                 this.rectArray[i] = rec;   
  70.             }   
  71.                
  72.             this.callBack(bmd,this.Index(0));   
  73.             //bmd.dispose();//释放bmd资源   
  74.                
  75.         }   
  76.            
  77.         public function Index(index:Number):Rectangle {   
  78.                
  79.             if (this.rectArray.length <= index) {   
  80.                 throw new Error("超过索引");   
  81.             }   
  82.             return this.rectArray[index];   
  83.         }   
  84.            
  85.         public function get RectArray():Array {   
  86.                
  87.             return this.rectArray;   
  88.         }   
  89.         public function get RowNum():uint {   
  90.             return this.rowNum;   
  91.         }   
  92.         public function get ListNum():uint {   
  93.             return this.listNum;   
  94.         }   
  95.         public function get Count():uint {   
  96.             return this.count;   
  97.         }   
  98.         public function toString() {   
  99.             return "lbynet bitamp array";   
  100.         }   
  101.            
  102.            
  103.     }   
  104.        
  105. }  

上面类我们主要存储BitmapData数据,如果是用ScrollRect,那我们存储的是Rectangle数据。

下面是测试:

AS3代码
  1. package     
  2. {   
  3.        
  4.     import flash.display.*;   
  5.     import flash.events.*;   
  6.     import flash.utils.Timer;   
  7.     /**  
  8.      * ...  
  9.      * @lbynet   
  10.      */  
  11.     public class Main extends Sprite  
  12.     {   
  13.         private var size:uint = 100;   
  14.         private var timer:Timer;   
  15.         private var bitmapArray:Array;   
  16.         private var gameContent:Sprite;   
  17.         private var qBitmapData:QBitmapData;   
  18.         private var bitmapData:BitmapData;   
  19.            
  20.         private var direction:uint;//方向   
  21.         private var index:uint;   
  22.         private var rowNum:uint;   
  23.         public function Main()    
  24.         {   
  25.             init();   
  26.         }   
  27.            
  28.         private function init():void {   
  29.                
  30.             gameContent = new Sprite();   
  31.             this.addChild(gameContent);   
  32.             gameContent.x = stage.stageWidth / 2 - this.size/2;   
  33.             gameContent.y = stage.stageHeight / 2 - this.size/2;   
  34.             this.qBitmapData = new QBitmapData("sprite.png"this.size);   
  35.             //this.qBitmapData = new QBitmapData("sprite.png", 100, gameContent);   
  36.             this.qBitmapData.callBack = callBack;   
  37.                
  38.         }   
  39.            
  40.         private function addListener():void {   
  41.                
  42.             this.timer = new Timer(100);     
  43.             this.timer.addEventListener(TimerEvent.TIMER, timerHandler);   
  44.                
  45.             stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);   
  46.         }   
  47.            
  48.         private function timerHandler(event:TimerEvent):void {   
  49.                
  50.             if (this.gameContent.numChildren>1) {   
  51.                 this.gameContent.removeChildAt(0);   
  52.             }   
  53.                
  54.             var btd:BitmapData = this.bitmapArray[this.direction*this.rowNum + this.index];   
  55.             var bm:Bitmap = new Bitmap(btd);   
  56.             gameContent.addChild(bm);   
  57.                
  58.             //角色动作控制   
  59.             if (this.index < this.rowNum - 1) {   
  60.                 this.index ++;   
  61.             } else {   
  62.                 this.index = 0;   
  63.             }   
  64.                
  65.             trace(gameContent.width);   
  66.         }   
  67.            
  68.         private function keyDownHandler(event:KeyboardEvent):void {   
  69.             switch (event.keyCode) {   
  70.                 case 40 :   
  71.                     this.direction = 0;   
  72.                     break;   
  73.                 case 38 :   
  74.                     this.direction = 3;   
  75.                     break;   
  76.                 case 37 :   
  77.                     this.direction = 1;   
  78.                     break;   
  79.                 case 39 :   
  80.                     this.direction = 2;   
  81.                     break;   
  82.             }     
  83.         }     
  84.            
  85.         private function callBack(bitmapArray:Array, btd:BitmapData):void {   
  86.             //trace("callBack function" + bitmapArray);   
  87.             //trace("callBack function" + btd);   
  88.                
  89.             this.bitmapArray = bitmapArray;   
  90.             this.rowNum = this.qBitmapData.RowNum;   
  91.             this.addListener();   
  92.                
  93.             this.timer.start();   
  94.                
  95.             //var btd2:BitmapData=_bitmap.Index(10);   
  96.             //var bm2:Bitmap=new Bitmap(btd2);   
  97.             //gameContent.addChild(bm2);   
  98.         }   
  99.            
  100.            
  101.     }   
  102.        
  103. }