这里仅仅是人物的渲染,鼠标点击控制人物的行走以及键盘4个方向键控制人物的行走。先说下注意点,以便以后犯错。
1丶坐标的转换,角度的计算。如果在渲染人物Char.as类中,进行人物的坐标移动,那么Char被实例化,添加的舞台的时候。其坐标一定要是(0,0)。要不,当鼠标点击舞台计算角度的时候会不正确。
2丶char 继承Sprite。将它添加到舞台的时候。如果要用到stage属性。就要用“Event.ADDED_TO_STAGE”事件,进行来侦听。否则会出错。如果你不用它,那就没事。
3丶角度,弧度的计算。以及人物方向的计算注意下就可以了。人物的渲染,资源消耗,效率方面也要注意。
文档类:Main.as
package { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; [SWF(width = "720",height = "450",frameRate = "30",backgroundColor = "0x000000")] public class Main extends Sprite { private var char:Char; public function Main():void { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; Init(); } private function Init() { char = new Char(this); addChild(char); //记得这里不要给char赋值坐标。要不char里边的计算角度会出现问题。 } } }
Char.as 类
package { import flash.display.Sprite; import flash.display.Bitmap; import flash.display.BitmapData; import flash.events.Event; import flash.events.MouseEvent; import flash.events.TimerEvent; import flash.geom.Rectangle; import flash.geom.Point; import flash.geom.Matrix; import flash.utils.Timer; import flash.ui.Keyboard; import KeyPoll; public class Char extends Sprite { private var _root; private var charArr:Vector.<Vector.<BitmapData > > = new Vector.<Vector.<BitmapData > > ; private var charBit:Bitmap; private var w_num:int = 8; private var h_num:int = 9; private var moveNum:int = 0;//当前人物运动的帧数 private var faceNum:int = 1;//当前人物脸的朝向。本来是8个方向的。多一个原地旋转 private var key:KeyPoll; private var controlMode:String = "Mouse"; private var myTimer:Timer; private var rate:int = 100; private var speed:int = 5; private var xspeed:Number = 4; private var yspeed:Number = 3; private var _target:Point = null; private var circle:Sprite; public static const DOWN:int = 1;//向下 public static const LEFT:int = 2;//向左 public static const UP:int = 4;//向上 public static const RIGHT:int = 3;//向右 public static const DOWN_LEFT:int = 5;//下左 public static const UP_LEFT:int = 7;//上左 public static const UP_RIGHT:int = 8;//上右 public static const DOWN_RIGHT:int = 6;//下右 public function Char(mc):void { _root = mc; if (stage) { addtoStageHandler(null); } else { addEventListener(Event.ADDED_TO_STAGE,addtoStageHandler); } } private function addtoStageHandler(e:Event=null) { if (hasEventListener(Event.ADDED_TO_STAGE)) { addEventListener(Event.ADDED_TO_STAGE,addtoStageHandler); } var men:BitmapData = new Men(0,0) as BitmapData; charArr = getChar(men); charBit=new Bitmap(); charBit.bitmapData = charArr[moveNum][faceNum]; charBit.x=(stage.stageWidth-charBit.width)*Math.random(); charBit.y=(stage.stageHeight-charBit.height)*Math.random(); addChild(charBit); key = new KeyPoll(stage); myTimer = new Timer(rate,0); myTimer.addEventListener(TimerEvent.TIMER,timerEventHandler); myTimer.start(); stage.addEventListener(MouseEvent.CLICK,clickHandler); circle=new Sprite(); circle.graphics.beginFill(0xff0000,0.6); circle.graphics.drawCircle(-3,-3,3); circle.graphics.endFill(); addChild(circle); circle.visible = false; } private function clickHandler(e:MouseEvent) { circle.visible = true; circle.x = e.stageX; circle.y = e.stageY; var radian:Number = Math.atan2(e.stageY - charBit.y - charArr[0][0].height,e.stageX - charBit.x - charArr[0][0].width / 2); var angle:Number=int(radian/Math.PI*180)+180; if (angle>=22 && angle<67) { faceNum = Char.UP_LEFT; } else if (angle>=67 && angle<112) { faceNum = UP; } else if (angle>=112 && angle<157) { faceNum = Char.UP_RIGHT; } else if (angle>=157 && angle<202) { faceNum = Char.RIGHT; } else if (angle>=202 && angle<247) { faceNum = Char.DOWN_RIGHT; } else if (angle>=247 && angle<292) { faceNum = Char.DOWN; } else if (angle>=292 && angle<337) { faceNum = Char.DOWN_LEFT; } else { faceNum = Char.LEFT; } _target = new Point(e.stageX - charArr[0][0].width / 2,e.stageY - charArr[0][0].height); xspeed = speed * Math.cos(radian); yspeed = speed * Math.sin(radian); trace(xspeed,yspeed); } private function timerEventHandler(e:TimerEvent) { moveNum++; if (moveNum>7) { moveNum = 0; } charBit.bitmapData = charArr[moveNum][faceNum]; controlKey(); if (controlMode=="Mouse") { if (_target==null ) { return; } if (Point.distance(new Point(charBit.x,charBit.y),_target) < 5) { circle.visible = false; _target = null; return; } charBit.x += xspeed; charBit.y += yspeed; } else { switch (faceNum) { case Char.DOWN_RIGHT : xspeed=speed * Math.cos(1/4 * Math.PI); yspeed=speed * Math.sin(1/4 * Math.PI); break; case Char.DOWN_LEFT : xspeed=speed * Math.cos(3/4 * Math.PI); yspeed=speed * Math.sin(3/4 * Math.PI); break; case Char.DOWN : xspeed=speed * Math.cos(1/2 * Math.PI); yspeed=speed * Math.sin(1/2 * Math.PI); break; case Char.UP_RIGHT : xspeed = speed * Math.cos(-1 / 4 * Math.PI); yspeed = speed * Math.sin(-1 / 4 * Math.PI); break; case Char.UP_LEFT : xspeed=speed * Math.cos(5/4 * Math.PI); yspeed=speed * Math.sin(5/4 * Math.PI); break; case Char.UP : xspeed = speed * Math.cos(-1 / 2 * Math.PI); yspeed = speed * Math.sin(-1 / 2 * Math.PI); break; case Char.RIGHT : xspeed=speed * Math.cos(0 * Math.PI); yspeed=speed * Math.sin(0 * Math.PI); break; case Char.LEFT : xspeed=speed * Math.cos(1 * Math.PI); yspeed=speed * Math.sin(1* Math.PI); break; } trace(xspeed,yspeed); charBit.x += xspeed; charBit.y += yspeed; } } /** * @ 键盘控制的八个方向呀。 */ private function controlKey() { controlMode = "keyboard"; if (key.isDown(Keyboard.DOWN)) { if (key.isDown(Keyboard.RIGHT)) { faceNum = Char.DOWN_RIGHT; } else if (key.isDown(Keyboard.LEFT)) { faceNum = Char.DOWN_LEFT; } else { faceNum = Char.DOWN; } } else if (key.isDown(Keyboard.UP)) { if (key.isDown(Keyboard.RIGHT)) { faceNum = Char.UP_RIGHT; } else if (key.isDown(Keyboard.LEFT)) { faceNum = Char.UP_LEFT; } else { faceNum = Char.UP; } } else if (key.isDown(Keyboard.LEFT)) { faceNum = Char.LEFT; } else if (key.isDown(Keyboard.RIGHT)) { faceNum = Char.RIGHT; } else { controlMode = "Mouse"; } } /** * 获取人物的各个动作位图 * @param BitmapData * @return Vector.<Vector.<BitmapData>> */ private function getChar(bitData:BitmapData):Vector.<Vector.<BitmapData > > { var bitDataArr:Vector.<Vector.<BitmapData > > = new Vector.<Vector.<BitmapData > > ; var w_width:Number = bitData.width / w_num; var h_height:Number = bitData.height / h_num; for (var i:int=0; i<w_num; i++) { var arr:Vector.<BitmapData > = new Vector.<BitmapData > ; for (var j:int=0; j<h_num; j++) { var bitmapData:BitmapData = new BitmapData(w_width,h_height,true,0); bitmapData.copyPixels(bitData,new Rectangle(w_width*i,h_height*j,w_width,h_height),new Point(0,0),null,null,true); arr.push(bitmapData); } bitDataArr.push(arr); } return bitDataArr; } private function getCharOther(bitData:BitmapData):Vector.<Vector.<BitmapData > > { var bitDataArr:Vector.<Vector.<BitmapData > > = new Vector.<Vector.<BitmapData > > ; var w_width:Number = bitData.width / w_num; var h_height:Number = bitData.height / h_num; var mx:Matrix=new Matrix(); for (var i:int=0; i<w_num; i++) { var arr:Vector.<BitmapData > = new Vector.<BitmapData > ; for (var j:int=0; j<h_num; j++) { var bitmapData:BitmapData = new BitmapData(w_width,h_height,true,0); mx.tx = - w_width * i; mx.ty = - h_height * j; bitmapData.draw(bitData,mx); arr.push(bitmapData); } bitDataArr.push(arr); } return bitDataArr; } } }
KeyPoll.as 类
package { import flash.events.KeyboardEvent; import flash.events.Event; import flash.display.DisplayObject; import flash.utils.ByteArray; public class KeyPoll { private var states:ByteArray; private var dispObj:DisplayObject; public function KeyPoll( displayObj:DisplayObject ) { states = new ByteArray(); states.writeUnsignedInt( 0 ); states.writeUnsignedInt( 0 ); states.writeUnsignedInt( 0 ); states.writeUnsignedInt( 0 ); states.writeUnsignedInt( 0 ); states.writeUnsignedInt( 0 ); states.writeUnsignedInt( 0 ); states.writeUnsignedInt( 0 ); dispObj=displayObj; dispObj.addEventListener( KeyboardEvent.KEY_DOWN, keyDownListener, false, 0, true ); dispObj.addEventListener( KeyboardEvent.KEY_UP, keyUpListener, false, 0, true ); dispObj.addEventListener( Event.ACTIVATE, activateListener, false, 0, true ); dispObj.addEventListener( Event.DEACTIVATE, deactivateListener, false, 0, true ); } private function keyDownListener( ev:KeyboardEvent ):void { states[ ev.keyCode >>> 3 ] |= 1 << (ev.keyCode & 7); } private function keyUpListener( ev:KeyboardEvent ):void { states[ ev.keyCode >>> 3 ] &= ~(1 << (ev.keyCode & 7)); } private function activateListener( ev:Event ):void { for (var i:int = 0; i < 32; ++i) { states[i]=0; } } private function deactivateListener( ev:Event ):void { for (var i:int = 0; i < 32; ++i) { states[i]=0; } } public function isDown( keyCode:uint ):Boolean { return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) != 0; } public function isUp( keyCode:uint ):Boolean { return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) == 0; } } }