package { import flash.display.Sprite; import flash.display.BitmapData; import flash.display.Bitmap; import flash.geom.Rectangle; import flash.geom.Point; import flash.events.*; //移动背景的类 public class MoveBg extends Sprite { //背景宽度 public var bgWidth:int; //背景高度 public var bgHeight:int; //源图像宽度 private var sourceBmpWidth:int; //画布 private var canvas:BitmapData; private var bitmapData:BitmapData; private var sourceRect:Rectangle; private var pt:Point=new Point(); private var bitmap:Bitmap; private var offX:int; private var currentX:int; public function MoveBg(w:int,h:int) { this.bgWidth = w; this.bgHeight = h; sourceRect = new Rectangle(0,0,w,h); canvas = new BitmapData(w,h); bitmap = new Bitmap(canvas); addChild(bitmap); } public function start():void { if(!this.hasEventListener(Event.ENTER_FRAME)) addEventListener(Event.ENTER_FRAME,onUpdate); } public function stop():void { if(this.hasEventListener(Event.ENTER_FRAME)) removeEventListener(Event.ENTER_FRAME,onUpdate); } private function onUpdate(event:Event):void { move(); } public function setSource(bitmapData:BitmapData,mouse_Enabled:Boolean=false):void { this.bitmapData = bitmapData; this.mouseEnabled = mouse_Enabled; this.sourceBmpWidth = bitmapData.width; } //移动背景 public function move():void { currentX++; offX = currentX % sourceBmpWidth;//偏移的位置 if (offX+bgWidth<=sourceBmpWidth) { sourceRect.x = offX; sourceRect.width = bgWidth; } else { sourceRect.x = offX; sourceRect.width = sourceBmpWidth - offX; } pt.x = 0; canvas.copyPixels(bitmapData, sourceRect, pt); if (offX >bgWidth) { pt.x = sourceBmpWidth - offX; canvas.copyPixels(bitmapData, canvas.rect, pt); } } } } 之前写的一个很小的东西,现在小封装一下,对一些手机图片滚屏效果只是需要用到一张图片来达到循环播放的效果的游戏http://www.shengshiyouxi.com。基本的原理是采取图片裁剪像素的方式来拼凑这图片。目前可以用于一些小游戏开始窗口背景滚屏的效果。 有兴趣可以尝试一下,明天贴一下图片效果。 |
flash与动画:图片移动实现新探索
最新推荐文章于 2024-09-05 09:25:24 发布