flash与动画:图片移动实现新探索
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);
}
}
}
补充:综合编程 , 其他综合 ,