package { import com.bit101.components.Label; import com.bit101.components.NumericStepper; import flash.display.MovieClip; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.filters.GlowFilter; import net.hires.debug.Stats; [SWF(frameRate = "60", width = "720", height = "480", backgroundColor = "#000000")] public class BitmapPerformance extends MovieClip { public static const FIRST_NUM:uint = 1000; private var ns:NumericStepper; public function BitmapPerformance() { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; addChild(new StarlingPart()); addChild(new Stats()); var label:Label = new Label(this, 80, 0, "Flash Bitmap Object Test"); label.scaleX = label.scaleY = 2; label.filters = [ new GlowFilter(0xFFFFFF, 1, 4, 4, 20)]; ns = new NumericStepper(this, 80, 40, changeHandler); ns.minimum = 250; ns.value = FIRST_NUM; ns.step = 250; } private function changeHandler(e:Event):void { StarlingPart.instance.reset(ns.value); } } } import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; import flash.display3D.textures.Texture; import flash.events.Event; import flash.geom.Rectangle; internal final class StarlingPart extends Sprite { public static var instance:StarlingPart; private static const STAGE_H:int = 480; private static const STAGE_W:int = 720; private var first:Particle; private var texture:BitmapData; public function StarlingPart() { instance = this; addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); } public function reset(numObj:uint):void { removeChildren(); var old:Particle; for (var i:int = 0; i < numObj; i++) { var p:Particle = new Particle(texture); addChild(p); p.x = stage.stageWidth * Math.random(); p.y = 0; p.vx = 0; p.vy = 10 * Math.random(); if (old) old.next = p; if (i == 0) first = p; old = p; } } private function addedToStageHandler(event:Event):void { texture = new BitmapData(16, 16, true, 0xFFFF0000); texture.fillRect(new Rectangle(1, 1, 14, 14), 0x0); reset(BitmapPerformance.FIRST_NUM); addEventListener(Event.ENTER_FRAME, enterFrameHandler); } private function enterFrameHandler(event:Event):void { var p:Particle = first; while (p.next) { p.x += p.vx; p.y += p.vy; if (p.x < 0) p.x = STAGE_W; else if (p.x > STAGE_W) p.x = 0; if (p.y < 0) p.x = STAGE_H; else if (p.y > STAGE_H) p.y = 0; p = p.next; } } } internal final class Particle extends Bitmap{ public var next:Particle; public var vx:Number = 0; public var vy:Number = 0; public function Particle(texture:BitmapData) { super(texture); } }