在上一篇 鼠标跟随基础上扩展游戏人物走动

主要要点是 人物行走 人物行走状态改变

     /** 更变人物动画行走方向*/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)*radiusangle = 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,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,true,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,loader.height,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)*radiusangle = Math.atan2(dy,dx)*180/Math.PI;*/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=

flash actionscript3.0 游戏人物走动相关推荐

  1. 夏敏捷第28本著作《Flash ActionScript3.0动画基础与游戏设计》(Flash CC版)

            二十 年来,多媒体技术飞速发展,已成为计算机和网络的必备功能.Flash是2D动画制作不可或缺的工具,它以色彩.形态.音效和音乐展现媒体技术的魅力.经过多年的发展,Adobe Flas ...

  2. ActionScript3.0自定义Flex组件问题 重写组件的使用

    最近在做Flex的一些学习,需要对Flex组件重写,当然可以两种选择MXML和ActionScript3.0重写,当然MXML的可视化的操作为重写提供了方便,但是要是更改组件的默认属性和添加一些框架属 ...

  3. actionscript 3.0 怎么写android 程序,(ActionScript3.0笔记)第一个程序HelloWorld!

    (ActionScript3.0笔记)第一个程序HelloWorld! 创建我的第一个ActionScript3.0程序--HelloWord! 首先下载ActionScript3.0的集成开发环境, ...

  4. html插入flash时钟,教你利用Flash制作一个会走动的时钟(时针,分针,秒针)

    其实制作时钟的方法很多,在此小编利用Flash也制作一个时钟,就当是学习CS3语言的编程技巧吧.下面是具体的实现步骤. 工具/原料 Adobe Flash CS3 方法/步骤 1.新建一Flash文档 ...

  5. Flash AS3.0实战

    如今网页游戏在游戏产业中占有半壁江山.在网页游戏中,百分之九十使用的是flash as3来做前端交互的开发.flash以其体积小等特性,吸引了无数的玩家.比如<傲剑>,<神仙道> ...

  6. 《点睛:ActionScript3.0游戏互动编程》——第2章 融会贯通—大话图层样式与滤镜2.1 Photoshop图层样式初体验...

    本节书摘来自异步社区<点睛:ActionScript3.0游戏互动编程>一书中的第2章,第2.1节,作者:游志德 , 彭文波 更多章节内容可以访问云栖社区"异步社区"公 ...

  7. 基于ActionScript3.0的DoodleJump 游戏实现

    基于ActionScript3.0的DoodleJump 游戏实现 <附加>: 分工: 王钊:游戏整体设计       陈旻:算法细节实现 1.流程图: 2.UML图: 3.为了游戏性和稳 ...

  8. Flash As3.0 游戏开发小结

    转自: http://blog.csdn.net/chongtianfeiyu/article/details/8096446 ActionScript3.0(以下简称AS3.0)开发flash游戏目 ...

  9. 【百行代码说游戏】ActionScript3.0 小游戏 【劲舞团】源码+演示

    最近学ActionScript3.0  以下为自己写的一个小游戏.尽量以最少的代码,实现功能 游戏原理:看代码注释 游戏规则:类似于[劲舞团]游戏,玩家可以按UP,DOWN,LEFT,RIGHT键来操 ...

最新文章

  1. 转录本counts,FPKM,TPM相互转化
  2. 数学建模记录(如何组织,如何参加)(一)
  3. IEnumeratorTItem和IEnumerator Java 抽象类和普通类、接口的区别——看完你就顿悟了...
  4. matlab访问数组的元素,使用分类数组访问数据
  5. 二进制位交换,反转,与统计1的个数
  6. java如何写安卓接口文档_android、java制作sdk以及自动生成文档
  7. 28岁成中科院课题组长,最近他接连在Nature和Science发文
  8. jquary插件Lightbox灯箱
  9. cropper裁剪图片并上传
  10. 合并的表格怎么加横线_怎么在表格中加一横线
  11. 服务器性能监控主要内容,主要服务器的各项指标监控
  12. oracle spatial 11g 安装,手动安装Oracle Spatial
  13. Table [xx] contains physical column name referred to by multiple physical column names 错误处理
  14. 解决mac休眠睡眠异常耗电方法
  15. phalapi门店管理系统插件,门店erp系统
  16. 第一次写正规论文的同学务必分享,排版自动排版,加注释。。。。。。等等!!!!!!!!(copy也得整出档次来啊,这就是门面啊)
  17. matlab 强度梯度,梯度算法的Matlab实现
  18. 《过目不忘的读书方法》读书摘要
  19. C# 实现虚拟打印机 HP Color LaserJet 4500 (2) True Type Font字体显示
  20. Proteus仿真STM32F103R6(一)

热门文章

  1. python输出一个月日历表_Python实例教程之检索输出月份日历表
  2. 【SequoiaDB巨杉数据库】特殊类型对象-BinData
  3. 使用python实现矩阵
  4. Memcache 键值key的格式和类型
  5. baocms伪静态_baocms7.7 本地生活系统无限制版 功能强大带农家乐版本
  6. 接收到的CAD图纸如何查看其版本信息?
  7. 删除照片恢复,最实用的方法快收藏起来!
  8. 新斗罗大陆手游服务器维护,《新斗罗大陆》4月20日合服公告
  9. 第一行代码Java课后习题学习
  10. PCL——从点云到网格(三)点云到Mesh