关闭

AS3 缓制粒子时,最好使用,数据源与显示分离。非一般的高性能

649人阅读 评论(0) 收藏 举报
package {
	
	//imports
	import flash.events.Event;
	import flash.display.Bitmap;
	import flash.utils.getTimer;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	
	public class Bitmapvs_MovieClip extends MovieClip {
		
		//define variables
		private var w:int;
		private var h:int;
		private var r:int = 5;
		private var n:int = 500;
		private var currentTime:int = 0;
		private var bm:Bitmap;
		private var bmd:BitmapData;
		private var bmShape:MovieClip = new MovieClip();
		
		public function Bitmapvs_MovieClip():void {
			
			w = stage.stageWidth;
			h = stage.stageHeight;
			bmd = new BitmapData(w, h, true, 0);
			bm = new Bitmap(bmd);
			addChild(bm);
			addEventListener(Event.ENTER_FRAME, onFrameLoop);
		}
		private function onFrameLoop (evt:Event):void {
			timeDifference;
			//use one of these functions at a time
			
			//this one is to create points using the movieclip method
			//createMovieClipCircles();
			
			//this one is to create points using the bitmap method
			createBitmapCircles();
		}
		private function createMovieClipCircles ():void {
			
			//this will create a new movieclip for each circle
			for (var i:int = 0; i < n; i ++) {
				var m:MovieClip = new MovieClip();
				drawCircle(m);
				addChild(m);
			}
			//trace the fps
			trace(1000 / timeDifference);
		}
		private function createBitmapCircles ():void {
			
			//this will draw the same movieclip in different places over and over
			bmd.lock();
			for (var j:int = 0; j < n; j ++) {
				drawCircle(bmShape);
				bmd.draw(bmShape);
			}
			bmd.unlock();
			//trace the fps
			trace(1000 / timeDifference);
		}
		private function drawCircle (m:MovieClip):void {
			
			//create the same function to draw circles for both to keep it fair and organised
			m.graphics.clear();
			m.graphics.beginFill(Math.random() * 0xffffff);
			m.graphics.drawCircle(Math.random() * w, Math.random() * h, r);
			m.graphics.endFill();
		}
		private function get timeDifference ():int{
			var totalPlayedTime:int = getTimer();
			var timeDifference:int = (totalPlayedTime - currentTime);
			currentTime = getTimer();
			return timeDifference
		}
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    文章分类
    最新评论