package { import flash.display.Sprite; public final class SpritePool { //定义对象池的最大尺寸 private static var MAX_VALUE:uint; //对象池增长补偿 不能缺一个 就补一个吧 private static var GROWTH_VALUE:uint; //当前对象池里空闲可用的对象数目 private static var counter:uint; //对象池 private static var pool:Vector.<Sprite>; private static var currentSprite:Sprite; //初始化对象池 存取满对象 public static function initialize( maxPoolSize:uint, growthValue:uint ):void { MAX_VALUE = maxPoolSize; GROWTH_VALUE = growthValue; counter = maxPoolSize; var i:uint = maxPoolSize; pool = new Vector.<Sprite>(MAX_VALUE); while( --i > -1 ) pool[i] = new Sprite(); } public static function getSprite():Sprite { if ( counter > 0 ) return currentSprite = pool[--counter]; //如果对象池存量不够了 则再续 GROWTH_VALUE 个新对象补充 var i:uint = GROWTH_VALUE; while( --i > -1 ) pool.unshift ( new Sprite() ); counter = GROWTH_VALUE; return getSprite(); } public static function disposeSprite(disposedSprite:Sprite):void { pool[counter++] = disposedSprite; } } }