用Bitmap/BitmapData 实现人物行走

来源:互联网 发布:芒果tv网络连接失败 编辑:程序博客网 时间:2024/05/01 20:24

///////////////////////////////////////main.as

package flash023{

import flash.display.BitmapData;

import flash.geom.Rectangle;

import flash.display.Loader;

import flash.net.URLRequest;

import flash.display.Bitmap;

import flash.events.Event;

import flash.display.Sprite;

import flash.geom.Point;

//import flash023.JPEGMovieClip;

public class Main extends Sprite {

private var loader:Loader;

public function Main():void {

loader=new Loader;

loader.contentLoaderInfo.addEventListener(Event.COMPLETE ,onJPEGLoaderComplete);

try {

loader.load(new URLRequest("mode.jpg"));

} catch (_e:Error) {

trace(_e);

}

}

public function onJPEGLoaderComplete(_evt:Event):void {

var _bmp:Bitmap =_evt.target.content as Bitmap;

var main_bmd:BitmapData = _bmp.bitmapData.clone();

loader.unload();

createBmdArray(main_bmd);

}

public function createBmdArray(_bmd:BitmapData):void{

var posW:uint = _bmd.width / 6;

var posH:uint = _bmd.height / 8;

for (var _h:uint = 0; _h < 2; _h++) {

var _mc:JPEGMovieClip=new JPEGMovieClip;

_mc.x = Math.random()*900;

_mc.y = Math.random()*500;

addChild(_mc);

for (var _w:uint = 0; _w < 6; _w++) {

var _temp:BitmapData = new BitmapData(posW, posH);

var _rec:Rectangle = new Rectangle(1 + _w * posW, _h * posH, posW, posH);

_temp.copyPixels(_bmd,_rec,new Point());

_mc.pushFrame(_temp);

}

}

}

}

}

////////////////////////JPEGMovieClip.as

package flash023{

import flash.display.Bitmap;

import flash.display.BitmapData;

import flash.display.Sprite;

import flash.events.Event;

public class JPEGMovieClip extends Sprite {

private var bmd_array:Array;

private var total:uint;

private var current:uint;

private var bitmap:Bitmap;

private var d:int = 0;

public function JPEGMovieClip() {

this.scaleX = 0.5;

this.scaleY = 0.5;

bmd_array=new Array  ;

total=1;

current=0;

bitmap=new Bitmap;

addChild(bitmap);

addEventListener(Event.ENTER_FRAME,show_func);

}

public function get totalframes():uint {

return total;

}

public function get currentframe():uint {

return current + 1;

}

private function show_func(_evt:Event):void{

d++;

if(d==3){

current%= total;

bitmap.bitmapData=bmd_array[current++];

d = 0;

this.x +=5;

//this.y +=2;

if(this.x > 920){

if(this.y > 560){

this.y = 10;

}

this.x = -10;

}

}

}

public function pushFrame(_bmd:BitmapData):void{

bmd_array.push(_bmd);

total=bmd_array.length;

}

}

}