接着上一篇,用点击实现角色人物的行走:
给出生成文件:
贴出AS:
1 package 2 { 3 import flash.display.Sprite; 4 import flash.events.Event; 5 import flash.events.MouseEvent; 6 import flash.text.TextField; 7 import flash.text.TextFormat; 8 9 /** 10 * @author vini 11 * @ web http://blog1.vini123.com 12 * @explanation 刚看了下键盘控制人物角色的行走,就想起用鼠标点击来实现。所以就总结了下这个。 13 */ 14 [SWF(width = "720",height = "450",frameRate = '30',backgroundColor = "0x3366ee")] 15 public class Main extends Sprite 16 { 17 private var isDraw:Draw; 18 private var poArr:Array = []; 19 private var lock:Boolean = false; 20 private var txt:TextField; 21 22 23 public function Main():void 24 { 25 addEventListener(Event.ADDED_TO_STAGE,addtoStageHandler); 26 } 27 28 private function addtoStageHandler(e:Event) 29 { 30 removeEventListener(Event.ADDED_TO_STAGE,addtoStageHandler); 31 32 var txtFormat:TextFormat=new TextFormat(); 33 txtFormat.size = 14; 34 txt=new TextField(); 35 txt.setTextFormat(txtFormat); 36 txt.selectable = false; 37 txt.mouseEnabled = false; 38 txt.textColor = 0xff0000; 39 txt.width = stage.stageWidth - 20; 40 txt.multiline = true; 41 txt.x = 10; 42 txt.y = 20; 43 addChild(txt); 44 45 isDraw = new Draw(this); 46 addChild(isDraw); 47 showMes(false,"请点击舞台:") 48 stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 49 } 50 51 private function mouseDownHandler(e:MouseEvent) 52 { 53 poArr.push(e.stageX,e.stageY); 54 if (! lock) 55 { 56 lock = true; 57 isDraw.setCircle(poArr[0],poArr[1]); 58 showMes(false,"选择起始点:坐标【" + poArr[0] + "," + poArr[1] + "】"); 59 } 60 else if (lock) 61 { 62 lock = false; 63 var radian:Number = Math.atan2(poArr[3] - poArr[1],poArr[2] - poArr[0]); 64 var angle:Number = radian / Math.PI * 180 + 180; 65 poArr.push(int(angle)); 66 showMes(false,"",poArr); 67 isDraw.drawLine(poArr[0],poArr[1],poArr[2],poArr[3]); 68 poArr = []; 69 } 70 } 71 72 public function showMes(ok:Boolean,str:String,arr:Array=null) 73 { 74 if (ok) 75 { 76 txt.appendText(str); 77 return; 78 } 79 if (str=="") 80 { 81 txt.text = "人物从坐标:【" + arr[0] + "," + arr[1] + "】出发,欲要到达:【" + arr[2] + "," + arr[3] + "】,人物行走的角度是:" + arr[4]; 82 } 83 else 84 { 85 txt.text = str; 86 } 87 88 } 89 } 90 }
1 package 2 { 3 import flash.display.Sprite; 4 import flash.display.Shape; 5 import flash.geom.Point; 6 import com.greensock.TweenLite; 7 8 public class Draw extends Sprite 9 { 10 private var line:Shape;//线条 11 private var circle:Shape;//圆形 12 private var speed:Number = 30;//速度 13 private var _root; 14 15 public function Draw(mc) 16 { 17 _root = mc; 18 line=new Shape(); 19 circle=new Shape(); 20 21 addChild(line); 22 addChild(circle); 23 drawCircle(); 24 } 25 26 public function drawLine(begin_x,begin_y,end_x,end_y) 27 { 28 line.graphics.lineStyle(2,0xff0000,0.6); 29 line.graphics.moveTo(begin_x,begin_y); 30 line.graphics.lineTo(end_x,end_y); 31 32 var targetPoint:Point = new Point(begin_x,begin_y); 33 var curPoint:Point = new Point(end_x,end_y); 34 var distance:Number = Point.distance(curPoint,targetPoint); 35 var timer:Number = distance / speed; 36 _root.showMes(true,(",走路需要时间约:"+int(timer))); 37 trace("时间!"); 38 TweenLite.to(circle,timer,{x:end_x,y:end_y,onComplete:onCompleteHandler}); 39 } 40 41 private function onCompleteHandler() 42 { 43 _root.showMes(false,"走路应完成"); 44 } 45 46 private function drawCircle() 47 { 48 circle.graphics.beginFill(0x00ff00,0.5); 49 circle.graphics.drawCircle(0,0,5); 50 circle.graphics.endFill(); 51 } 52 public function setCircle(_x,_y) 53 { 54 line.graphics.clear(); 55 TweenLite.killTweensOf(circle,true); 56 circle.x = _x; 57 circle.y = _y; 58 } 59 } 60 }