继续让子弹飞,传统方式实现

昨天优化了让子弹飞的效果 http://www.cnblogs.com/heycup/archive/2011/01/25/1944704.html

为了测试各种情况下占用资源的情况,今天用传统方式实现了同样的效果

子弹数量由原来的500变成5000,经测试,二者消耗相同,5000子弹cpu占用均为15%,内存上本实现稍微多点,但是相差不多

下面是代码,moveobject类可以在昨天的代码中找到

 

package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * ...
	 * @author heycup@gmail.com
	 */
	public class FlyBullet extends Sprite {
		public static var w:int = 600;
		public static var h:int = 400;
		private var objects:Vector.<MoveObject> = new Vector.<MoveObject>();
		private var bullets:Vector.<Bitmap> = new Vector.<Bitmap>();
		private var maxNum:uint = 5000;

		//嵌入子弹图片
		[Embed(source='../bin/zhidan.png')]
		private var ZhiDan:Class;
		private var zhidanBmd:BitmapData;

		public function FlyBullet(){
			init();
		}

		private function init():void {
			//获取子弹的bitmapdata
			var zhidan:Bitmap = new ZhiDan();
			zhidanBmd = zhidan.bitmapData;

			//实例大量运动类及子弹
			for (var i:int = 0; i < maxNum; i++){
				var moveObject:MoveObject = new MoveObject();
				moveObject.x = w * Math.random();
				moveObject.y = h * Math.random();
				moveObject.sx = Math.random() * 5 + 1;
				moveObject.sy = Math.random() * 5 + 1;
				objects.push(moveObject);

				var bitmap:Bitmap = new Bitmap(zhidanBmd);
				bitmap.x = moveObject.x;
				bitmap.y = moveObject.y;
				addChild(bitmap);
				bullets.push(bitmap);
			}
			//开始运动
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(e:Event):void {
			for (var i:int = 0, n:int = bullets.length; i < n; i++){
				var bullet:Bitmap = bullets[i];
				var moveObject:MoveObject = objects[i];
				bullet.x = moveObject.x;
				bullet.y = moveObject.y;
				moveObject.update();
			}
		}
	}

}

 

posted @ 2011-01-26 11:13  heycup  阅读(467)  评论(2编辑  收藏  举报