starling性能問題筆記(二)

package
{
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.textures.Texture;
	
	public class JustATest extends Sprite
	{
		[Embed(source="1.png")]
		public static var A_Bitmap:Class;

		private var t1:Texture;

		private var img:Image;
		private var oldImages:Array = [];
		public function JustATest()
		{
			addEventListener(Event.ADDED_TO_STAGE,onAdd);
		}
		
		private function onAdd(e:Event):void
		{
			t1 = Texture.fromBitmap(new A_Bitmap(),false);
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		private function onEnterFrame(e:Event):void
		{
			//清理
			for (var j:int = 0; j < oldImages.length; j++){
				oldImages[j].visible = false;
				var iii:Image = oldImages[j] as Image;
				iii.texture.dispose();//如果註釋這一句,顯卡很快就會爆掉,所以,用完Texture.fromBitmap,在退出場景的時候,一定要dispose,否則無用的、重複的貼圖,就會佔滿顯卡。
			}
			oldImages = [];
			
			//繪製
			for (var i:int = 0; i < 10; i++){
				img = new Image(Texture.fromBitmap(new A_Bitmap(),false));
				oldImages.push(img);
				img.x = Math.random()*(stage.stageWidth-img.width);
				img.y = Math.random()*(stage.stageHeight-img.height);
				addChild(img);
			}
		}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章