
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.utils.Timer;
public class Main extends MovieClip {
var bm:Bitmap;
var playerDataArr:Array;
var arrIndex = -1;
public function Main() {
// constructor code
var urlRequest:URLRequest = new URLRequest("player.png");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
loader.load(urlRequest);
}
private function onComplete(e:Event):void{
var l:LoaderInfo = e.target as LoaderInfo;
this.bm = l.content as Bitmap;
playerDataArr = bitmapDataSlice(bm.bitmapData,4,4);
bm.bitmapData = null;
this.addChild(bm);
var timer:Timer = new Timer(200,0);
timer.addEventListener(TimerEvent.TIMER,onWalk);
timer.start();
}
private function onWalk(e:TimerEvent):void{
this.arrIndex++;
if (arrIndex>3) this.arrIndex = 0;
this.bm.bitmapData = playerDataArr[0][arrIndex];
}
//位图切割函数
private function bitmapDataSlice(sourceBitmapData:BitmapData,rows:uint,column:uint):Array{
var imgWidth:int = sourceBitmapData.width/column;
var imgHeight:int = sourceBitmapData.height/rows;
var bufferArr:Array = new Array();
for(var i=0;i<rows;i++){
var stepArr:Array= new Array();
for(var j=0;j<column;j++){
var buffer:BitmapData = new BitmapData(imgWidth,imgHeight);
buffer.copyPixels(sourceBitmapData,new Rectangle(j*imgWidth,i*imgHeight,imgWidth,imgHeight),new Point(0,0));
stepArr.push(buffer);
}
bufferArr.push(stepArr);
}
return bufferArr;
}
}
}