Last active
December 10, 2015 16:28
-
-
Save JulianG/4460785 to your computer and use it in GitHub Desktop.
General purpose AS3 Object Pool class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
/** | |
* ObjectPool Class | |
* @author Julian | |
*/ | |
public class ObjectPool | |
{ | |
private var _list:Array; | |
public function ObjectPool(object_type:Class, qty:uint) | |
{ | |
for (var i:int = 0; i < qry; i++) | |
{ | |
_list.push( new object_type() ); | |
} | |
} | |
public function getNextObject():* | |
{ | |
var obj:* = _list.shift(); | |
_list.push(obj); | |
return obj; | |
} | |
} | |
} |
Nice one! There's a typo on line 13, qry instead of qty :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
If you want the object pool to be globally accessible, you don't need static methods.
You can create a package-level variable like this:
This way you can create many different instances of ObjectPool for different entities in your game without duplicating your code.