flash actionscript3.0 游戏人物走动
发布时间:2020-12-15 06:50:33 所属栏目:百科 来源:网络整理
导读:在上一篇 鼠标跟随基础上扩展游戏人物走动 主要要点是 人物行走 人物行走状态改变 /** 更变人物动画行走方向*/private function upfang(d:Number):void{ if(d=-0.524d=0.524) { sDirection = 2; } if(d=0.524d=1.05) { sDirection = 5; } if(d=1.05d=2.1) {
在上一篇 鼠标跟随基础上扩展游戏人物走动 主要要点是 人物行走 人物行走状态改变 /* * 更变人物动画行走方向 */ private function upfang(d:Number):void{ if(d>=-0.524&&d<=0.524) { sDirection = 2; } if(d>=0.524&&d<=1.05) { sDirection = 5; } if(d>=1.05&&d<=2.1) { sDirection = 0; } if(d>=2.1&&d<=2.62) { sDirection = 4; } if(d>=2.62||d<=-2.62) { sDirection = 1; } if(d>=-2.62&&d<=-2.09) { sDirection = 6; } if(d>=-2.09&&d<=-1.05) { sDirection = 3; } if(d>=-1.05&&d<=-0.524) { sDirection = 7; } } 行走方法是上一篇,用圆方式 if(plist.length>0&&sun.x==point.x&&sun.y==point.y) { point=plist.shift(); dx = point.x-sun.x; dy = point.y-sun.y; /* //根据三角函数求出弧度也是角度 ball.x=centerX+Math.cos(angle)*radius; ball.y=centerY+Math.sin(angle)*radius angle = Math.atan2(dy,dx)*180/Math.PI; */ angle=Math.atan2(dy,dx); upfang(angle); } package { import flash.display.Sprite; import flash.events.*; import flash.geom.*; import flash.filters.*; import flash.display.DisplayObject; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.net.URLRequest; import flash.net.*; import flash.utils.Timer; import flash.events.*; import flash.geom.*; public class dituyundong extends Sprite { private var sun,moon:Sprite=new Sprite();//创建容器 private var centerX:Number=stage.stageWidth/2; private var centerY:Number=stage.stageHeight/2; private var k,dx,dy,angle:Number=0; private var point:Point=new Point(); private var plist:Array=new Array(); private var loader,loaderp:Loader; private var timer:Timer; private var sWidth:uint; private var sHeight:uint; private var sStep:uint; private var sDirection:uint; private var maps:Array; private var pointer:uint; private var map:Bitmap; public function dituyundong() { //加载地图,先显示 loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeDitu); loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler); loader.load(new URLRequest("ditu.jpg")); //画人物容器 sun=new Sprite(); addChild(sun); sun.graphics.beginFill(0x00ffcc00,0); sun.graphics.drawRect(0,64,64); sun.graphics.endFill(); //人物位置在舞台中间 sun.x=centerX; sun.y=centerY; sun.z=0; point.x=sun.x; point.y=sun.y; //鼠标点中后停住 sun.addEventListener(MouseEvent.MOUSE_DOWN,clearStep); //倾听舞台鼠标点击 stage.addEventListener(MouseEvent.MOUSE_DOWN,Mousedown); //加入帧事件 addEventListener(Event.ENTER_FRAME,Run); //角色大小; sWidth = 64; sHeight = 64; //角色移动方向; sDirection = 0; //角色步数; sStep = 1; //角色动作数组; maps = new Array(); //初始化角色动作运行指针; pointer = 0; //初始化time; timer = new Timer(100); timer.addEventListener(TimerEvent.TIMER,timerHandler); //图片加载人物对象; loaderp = new Loader(); loaderp.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler); loaderp.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler); loaderp.load(new URLRequest("a1.png")); stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler); } //键盘事件,通过方向键更改角色移动方向; private function keyDownHandler(event:KeyboardEvent):void { switch (event.keyCode) { case 40 : case 98 : sDirection = 0; plist.push(new Point(sun.x,sun.y+10)); break; case 38 : case 104 : sDirection = 3; plist.push(new Point(sun.x,sun.y-10)); break; case 37 : case 100 : sDirection = 1; plist.push(new Point(sun.x-10,sun.y)); break; case 39 : case 102 : sDirection = 2; plist.push(new Point(sun.x+10,sun.y)); break; case 103 : sDirection = 6; plist.push(new Point(sun.x-10,sun.y-10)); break; case 105 : sDirection = 7; plist.push(new Point(sun.x+10,sun.y-10)); break; case 97 : sDirection = 4; plist.push(new Point(sun.x-10,sun.y+10)); break; case 99 : sDirection = 5; plist.push(new Point(sun.x+10,sun.y+10)); break; } } //定时器运行事件; private function timerHandler(event:Event):void { //删除旧的角色动作图像; if (map != null) { sun.removeChild(map); } //显示新的角色动作图像; map = new Bitmap(maps[sDirection][pointer]); map.x=-32; map.y=-32; sun.addChild(map); //角色动作循环处理; if (pointer < sStep-1) { pointer ++; } else { pointer = 0; } } //加载图片完成处理事件; private function completeHandler(event:Event):void { //根据图片的大小初始化BitmapData; /* * 注意如果你要保留原来的图片的透明度的话,必将transparent设置为true,同时设置填充色值的前两位为00; */ var sBmd:BitmapData = new BitmapData(loaderp.width,loader.height,true,0x00FFFFFF); sBmd.draw(loaderp); //计算移动步数; sStep = Math.floor(loaderp.width/sWidth); for (var j:uint = 0; j<Math.floor(loader.height/sHeight); j++) { var arr:Array = new Array(); for (var i:uint = 0; i<sStep; i++) { var bmd:BitmapData = new BitmapData(sWidth,sHeight,0x00FFFFFF); //获取单个角色的BitmapData对象; bmd.copyPixels(sBmd,new Rectangle(sWidth*i,sHeight*j,sWidth,sHeight),new Point(0,0)); arr.push(bmd); } //放入角色数组里; maps.push(arr); } //释放sBmd资源; sBmd.dispose(); //开始运行角色动作; timer.start(); } //错误处理事件; private function errorHandler(event:IOErrorEvent):void { trace("IOErrorEvent"); } private function completeDitu(event:Event):void { var sBmd:BitmapData = new BitmapData(loader.width,false,0xFFFFFF); sBmd.draw(loader); var bitmap:Bitmap=new Bitmap(sBmd); addChild(bitmap); setChildIndex(sun,numChildren-1); } public function clearStep(e:Event):void{ plist=new Array(); point.x=sun.x; point.y=sun.y; e.stopPropagation(); } public function Run(e:Event):void{ dx = point.x-sun.x; dy = point.y-sun.y; //如果小于5,设置为目标点 if(Math.abs(dx)<5&&Math.abs(dy)<5) { sun.x=point.x; sun.y=point.y; }else{ var oa:Number=angle; angle=Math.atan2(dy,dx); sun.x=sun.x+Math.cos(angle)*5; sun.y=sun.y+Math.sin(angle)*5; //对比下是否改变了角度 if(angle!=oa) { upfang(angle); } } if(plist.length>0&&sun.x==point.x&&sun.y==point.y) { point=plist.shift(); dx = point.x-sun.x; dy = point.y-sun.y; /* //根据三角函数求出弧度也是角度 ball.x=centerX+Math.cos(angle)*radius; ball.y=centerY+Math.sin(angle)*radius angle = Math.atan2(dy,dx); upfang(angle); } } /* * 更变人物动画行走方向 */ private function upfang(d:Number):void{ if(d>=-0.524&&d<=0.524) { sDirection = 2; } if(d>=0.524&&d<=1.05) { sDirection = 5; } if(d>=1.05&&d<=2.1) { sDirection = 0; } if(d>=2.1&&d<=2.62) { sDirection = 4; } if(d>=2.62||d<=-2.62) { sDirection = 1; } if(d>=-2.62&&d<=-2.09) { sDirection = 6; } if(d>=-2.09&&d<=-1.05) { sDirection = 3; } if(d>=-1.05&&d<=-0.524) { sDirection = 7; } } /* 记录鼠标位置 */ public function Mousedown(e:Event):void{ var p:Point=new Point(); p.x=stage.mouseX; p.y=stage.mouseY; plist.push(p); if(plist.length>5) { plist.pop(); plist.pop(); plist.push(p); } } } } 素材 到这里下载吧 http://code.google.com/p/queryphp/downloads/detail?name=yundemo.zip&can=2&q= (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |