`
hacker47
  • 浏览: 336456 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

AS3进阶:利用缓冲池显示图片

阅读更多
无论是开发游戏,还是普通的应用,熟悉bitmapdata类的用法是有很必要的。利用bitmapdata作为缓冲池来显示图片,就是一个很常见的做法。

package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author hacker47
	 */
	[SWF(width="800",height="600",frameRate="30",backgroundColor="0xFFFFFF")]
	public class Main extends Sprite 
	{
		private var imageCache:BitmapData;
		private var displayImageData:BitmapData;
		private var displayImage:Bitmap;
		private var displayRectTangle:Rectangle;
		private var startX:Number;
		private var startY:Number;
		private var endX:Number;
		private var endY:Number;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			displayRectTangle = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
			
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
			// entry point
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void {
				imageCache = Bitmap(e.target.content).bitmapData;
				displayImageData = new BitmapData(displayRectTangle.width, displayRectTangle.height);
				displayImageData.copyPixels(imageCache, displayRectTangle, new Point(0,0));
				displayImage = new Bitmap(displayImageData);
				addChild(displayImage);
				drawWinFrame();
				stage.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
			});
			loader.load(new URLRequest("flash-tree.jpg"));
		}
		
		private function onDown(e:MouseEvent):void {
			startX = mouseX;
			startY = mouseY;
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}
		
		private function onUp(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}
		
		private function onMove(e:MouseEvent):void {
			endX = mouseX;
			endY = mouseY;
			displayRectTangle.x -= endX - startX;
			displayRectTangle.y -= endY - startY;
			drawNewImageView();
			startX = endX;
			startY = endY;
			
		}
		
		private function onWheel(e:MouseEvent):void {
			displayRectTangle.y -= e.delta*10;
			drawNewImageView();
		}
		
		private function drawNewImageView():void {
			if (displayRectTangle.x < 0) {
				displayRectTangle.x = 0;
			}else if ((displayRectTangle.x + displayRectTangle.width) > imageCache.width) {
				displayRectTangle.x = imageCache.width-displayRectTangle.width;
			}
			
			if (displayRectTangle.y < 0) {
				displayRectTangle.y = 0;
			}else if ((displayRectTangle.y + displayRectTangle.height) > imageCache.height) {
				displayRectTangle.y = imageCache.height-displayRectTangle.height;
			}
			
			
			imageCache.lock();
			displayImageData.copyPixels(imageCache, displayRectTangle, new Point());
			imageCache.unlock();
		}
		
		private function drawWinFrame():void {
			var s:Sprite = new Sprite();
			s.graphics.lineStyle(1);
			s.graphics.moveTo(0, 0);
			s.graphics.drawRect(0,0,displayRectTangle.width,displayRectTangle.height);
			addChild(s);
		}
		
	}
	
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics