今天天气很热,没有太多精神去进行实验,今晚随便写了一下代码。记录一些笔记,看看上面的图片是不是很酷呢?因为这种人物动画只是利用时间间隔播放不同帧才会看到不同效果。原理非常简单,只是切换不同帧。
flash 里面 提供时间间隔利用Timer 类 ,或者Enter_Frame刷帧这样的方式 更换不同的图片的切换。我们网上搜索了一张这样的png的图片,里面包含了不同的动作。现在有一种比较傻瓜的方式实现。第一种采用外部加载的方式将图片进行切割4x4的方式导入去 并使用数组进行保存。
在播放图片的时候,使用Bitmap类当中属性进行切换,这种是其中一种 方式。切换不同的帧的时候会有几种的方式,这是是其中一种方式。
bitmap.bitmapData =位图数据;
在切换的时候,我们只是需要对图片从新指定数据则可以。
在这里,只是使用了一张位图。同时创建了一个bitmap对象用于承载位图数据。
现在是切割位图的类。这个类对一张大图切割复制成想要的等分数。调用的时候
array= BitmapSplice.Splice(bitmap.bitmapData,4, 4);则采用这种方式即可。
好,大概就是这样。马上要睡觉了!
package org.summerTree.image{ /* 切割位图类,返回一个二维数组*/ // version 1.0 版本 import flash.display.Sprite; import flash.display.Bitmap; import flash.display.BitmapData; import flash.geom.*; public class BitmapSplice { public function BitmapSplice() { } //进行切割 public static function Splice(bitmapdata:BitmapData,titleWidth:uint,titleHeight:uint):Array { var Step:Array=new Array();//存取步数数组 var iWidth:int=bitmapdata.width /titleWidth; var iHeight:int=bitmapdata.height /titleHeight; for (var i:uint=0; i < titleHeight; i++) { var array:Array=new Array(); for (var j:uint=0; j < titleWidth; j++) { var tempBMP:BitmapData=new BitmapData(iWidth,iHeight,true,0x00000000); tempBMP.copyPixels(bitmapdata,new Rectangle(j * iWidth,i * iHeight,iWidth,iHeight),new Point(0,0)); array.push(tempBMP); } Step.push(array); } bitmapdata.dispose(); return Step; } }}
package { import flash.display.MovieClip; import flash.display.Loader; import flash.events.*; import flash.net.*; import flash.utils.Timer; import flash.display.Bitmap; import org.summerTree.image.BitmapSplice; public class Main extends MovieClip { private var url:String="3.png"; private var timer:Timer; private var array:Array; private var bitmap:Bitmap; private var _x:int=0; private var _y:int=0; private var count:int=0; public function Main() { init(); } private function init():void { bitmap = new Bitmap();//用于显示人物 addChild(bitmap); bitmap.x=250; bitmap.y=100; //时间器,对图片实现切换的时间间隔 timer=new Timer(300); timer.addEventListener(TimerEvent.TIMER,onTimer); //外部加载的图片 var loader:Loader=new Loader(); loader.load(new URLRequest(url)); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete); } private function onTimer(event:TimerEvent):void { bitmap.bitmapData = array[_y][_x]; _x++; if (_x>3) { if (count==0) { count=1; _x=1; } else { count=0; _x=0; } _y++; if (_y>3) { _y=0; } } } private function onLoadComplete(event:Event):void { event.currentTarget.removeEventListener(Event.COMPLETE,onLoadComplete); var bitmap:Bitmap=event.currentTarget.content; //addChild(bitmap); array= BitmapSplice.Splice(bitmap.bitmapData,4, 4); //trace(array); timer.start(); //addEventListener(Event.ENTER_FRAME,Run); } //private function Run(event:Event):void// {// bitmap.bitmapData = array[_y][_x];// _x++; if (_x>3)// {// if (count==0)// {// count=1;// _x=1;// }// else// {// count=0;// _x=0;// }// _y++;// if (_y>3)// {// _y=0;// }// }// }// }}
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow