续列图划分
2009-10-13 17:43 宝宝合凤凰 阅读(308) 评论(0) 收藏 举报
用位图填充的方式取续列图进行划分, 也是一种位图切割,我们可以通过该原理来对单张图片进行角色动作化处理 , 不过有时候使用scrollRect 性能更佳。

AS3代码
- package
- {
- import flash.display.*;
- import flash.events.*;
- import flash.net.*;
- import flash.events.IOErrorEvent;
- import flash.system.System;
- import flash.geom.Rectangle;
- import flash.geom.Point;
- /**
- * ...
- * @lbynet
- */
- public class QBitmapData
- {
- private var bufferContainer:DisplayObjectContainer;
- private var imagePath:String;
- private var bitmapArray:Array;
- private var _loader:Loader;
- private var size:Number;
- private var rowNum:uint;
- private var listNum:uint;
- private var count:uint;
- public var callBack:Function;
- public function QBitmapData(imagePath:String, size:Number=16,_bufferContainer:DisplayObjectContainer = null):void {
- bufferContainer = _bufferContainer;
- this.ImagePath = imagePath;
- this.size = size;
- }
- public function set ImagePath(_path:String):void {
- var urlRequest:URLRequest = new URLRequest(_path);
- this._loader = new Loader();
- //this._loader.contentLoaderInfo.addEventListener(Event.OPEN, openHandler);
- //this._loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
- this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
- //this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
- this._loader.load(urlRequest);
- }
- private function completeHandler(event:Event):void {
- var target:DisplayObject = _loader.content as DisplayObject;
- var xpos:Number = -this.size;
- var ypos:Number = 0;
- var bmd:BitmapData = new BitmapData(target.width, target.height, true, 0);
- bmd.draw(target);
- this._loader.unload();
- this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
- this.rowNum = Math.floor(bmd.width / this.size);
- this.listNum = Math.floor(bmd.height / this.size);
- this.count = this.rowNum * this.listNum;
- //创建长度数组;
- bitmapArray = new Array(count);
- //
- var rec:Rectangle = new Rectangle(0, 0, this.size, this.size);
- var p:Point = new Point(0, 0);
- for (var i:Number = 0; i < count; i++) {
- var localBMD:BitmapData = new BitmapData(this.size, this.size);
- if (xpos == (bmd.width - this.size)) { //换行读下一张,像是一个二维数组
- ypos += this.size;
- xpos = -this.size;
- }
- xpos += this.size;
- rec.x = xpos;
- rec.y = ypos;
- localBMD.copyPixels(bmd, rec, p);
- this.bitmapArray[i] = localBMD;
- }
- bmd.dispose();//释放bmd资源
- this.callBack(this.bitmapArray,this.Index(0));
- }
- public function Index(index:Number):BitmapData {
- if (this.bitmapArray.length<=index) {
- throw new Error("超过索引");
- }
- return this.bitmapArray[index];
- }
- public function get BitmapArray():Array {
- return this.bitmapArray;
- }
- public function get RowNum():uint {
- return this.rowNum;
- }
- public function get ListNum():uint {
- return this.listNum;
- }
- public function get Count():uint {
- return this.count;
- }
- public function toString() {
- return "lbynet bitamp array";
- }
- }
- }
AS3代码
- package
- {
- import flash.display.*;
- import flash.events.*;
- import flash.net.*;
- import flash.events.IOErrorEvent;
- import flash.system.System;
- import flash.geom.Rectangle;
- import flash.geom.Point;
- /**
- * ...
- * @lbynet
- */
- public class QScrollRect
- {
- private var bufferContainer:DisplayObjectContainer;
- private var imagePath:String;
- private var rectArray:Array;
- private var _loader:Loader;
- private var size:Number;
- private var rowNum:uint;
- private var listNum:uint;
- private var count:uint;
- public var callBack:Function;
- public function QScrollRect(imagePath:String, size:Number=16,_bufferContainer:DisplayObjectContainer = null):void {
- bufferContainer = _bufferContainer;
- this.ImagePath = imagePath;
- this.size = size;
- }
- public function set ImagePath(_path:String):void {
- var urlRequest:URLRequest = new URLRequest(_path);
- this._loader = new Loader();
- this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
- this._loader.load(urlRequest);
- }
- private function completeHandler(event:Event):void {
- var target:DisplayObject = _loader.content as DisplayObject;
- var xpos:Number = -this.size;
- var ypos:Number = 0;
- var bmd:BitmapData = new BitmapData(target.width, target.height, true, 0);
- bmd.draw(target);
- this._loader.unload();
- this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
- this.rowNum = Math.floor(bmd.width / this.size);
- this.listNum = Math.floor(bmd.height / this.size);
- this.count = this.rowNum * this.listNum;
- //创建长度数组;
- this.rectArray = new Array(count);
- var rec:Rectangle;
- var p:Point = new Point(0, 0);
- for (var i:Number = 0; i < count; i++) {
- rec = new Rectangle(0, 0, this.size, this.size);
- if (xpos == (bmd.width - this.size)) { //换行读下一张,像是一个二维数组
- ypos += this.size;
- xpos = -this.size;
- }
- xpos += this.size;
- rec.x = xpos;
- rec.y = ypos;
- this.rectArray[i] = rec;
- }
- this.callBack(bmd,this.Index(0));
- //bmd.dispose();//释放bmd资源
- }
- public function Index(index:Number):Rectangle {
- if (this.rectArray.length <= index) {
- throw new Error("超过索引");
- }
- return this.rectArray[index];
- }
- public function get RectArray():Array {
- return this.rectArray;
- }
- public function get RowNum():uint {
- return this.rowNum;
- }
- public function get ListNum():uint {
- return this.listNum;
- }
- public function get Count():uint {
- return this.count;
- }
- public function toString() {
- return "lbynet bitamp array";
- }
- }
- }
上面类我们主要存储BitmapData数据,如果是用ScrollRect,那我们存储的是Rectangle数据。
下面是测试:
AS3代码
- package
- {
- import flash.display.*;
- import flash.events.*;
- import flash.utils.Timer;
- /**
- * ...
- * @lbynet
- */
- public class Main extends Sprite
- {
- private var size:uint = 100;
- private var timer:Timer;
- private var bitmapArray:Array;
- private var gameContent:Sprite;
- private var qBitmapData:QBitmapData;
- private var bitmapData:BitmapData;
- private var direction:uint;//方向
- private var index:uint;
- private var rowNum:uint;
- public function Main()
- {
- init();
- }
- private function init():void {
- gameContent = new Sprite();
- this.addChild(gameContent);
- gameContent.x = stage.stageWidth / 2 - this.size/2;
- gameContent.y = stage.stageHeight / 2 - this.size/2;
- this.qBitmapData = new QBitmapData("sprite.png", this.size);
- //this.qBitmapData = new QBitmapData("sprite.png", 100, gameContent);
- this.qBitmapData.callBack = callBack;
- }
- private function addListener():void {
- this.timer = new Timer(100);
- this.timer.addEventListener(TimerEvent.TIMER, timerHandler);
- stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
- }
- private function timerHandler(event:TimerEvent):void {
- if (this.gameContent.numChildren>1) {
- this.gameContent.removeChildAt(0);
- }
- var btd:BitmapData = this.bitmapArray[this.direction*this.rowNum + this.index];
- var bm:Bitmap = new Bitmap(btd);
- gameContent.addChild(bm);
- //角色动作控制
- if (this.index < this.rowNum - 1) {
- this.index ++;
- } else {
- this.index = 0;
- }
- trace(gameContent.width);
- }
- private function keyDownHandler(event:KeyboardEvent):void {
- switch (event.keyCode) {
- case 40 :
- this.direction = 0;
- break;
- case 38 :
- this.direction = 3;
- break;
- case 37 :
- this.direction = 1;
- break;
- case 39 :
- this.direction = 2;
- break;
- }
- }
- private function callBack(bitmapArray:Array, btd:BitmapData):void {
- //trace("callBack function" + bitmapArray);
- //trace("callBack function" + btd);
- this.bitmapArray = bitmapArray;
- this.rowNum = this.qBitmapData.RowNum;
- this.addListener();
- this.timer.start();
- //var btd2:BitmapData=_bitmap.Index(10);
- //var bm2:Bitmap=new Bitmap(btd2);
- //gameContent.addChild(bm2);
- }
- }
- }
浙公网安备 33010602011771号