| Package | org.ffilmation.utils |
| Class | public class objectPool |
Object pooling is the process of storing objects in a pool when they are not in use so as to avoid the overhead of creating them again and again. In AS3, objects that are tied to the library are expensive to create. By storing them in a pool for later use you can greatly reduce memory usage and save a handful of milliseconds every frame. ( This definition was copied from here http://mikegrundvig.blogspot.com/2007/05/as3-is-fast.html)
This class will return instancies of any given class. When an object is no longer used, if you return it to the pool it will be available to be used again. In a good implementation there would be some sort of factory interface and you would provide an initialization method for each class to ensure properties are properly initialized, but this is not the case here.
The engine uses this pool mainly to reuse Sprites and MovieClips, which are very expensive to instantiate. You can use it for you own classes if you want too
IMPORTANT!: This is a very simple implementation of an object Pool. There is no such thing as size control. If one of your scenes
allocated 100 Sprites, for example, once they are returned they will still exist inside the pool and use memory.
You can call objectPool.flush(Class) or objectPool.flush() at anytime to free these resources (after hiding an scene is a good moment).
I decided not to automate this process for now because I'm not sure about the real scenarios this engine will face yet. I may do it in future
releases.
See also
| Method | Defined by | ||
|---|---|---|---|
|
flush(c:Class = null):void
[static]
Use this method delete stored instances and free some memory
| objectPool | ||
|
getInstanceOf(c:Class):Object
[static]
This method returns an instance of a given Class.
| objectPool | ||
|
returnInstance(object:Object):void
[static]
Use this method to return unused objects to the pool and make them available to be used again later.
| objectPool | ||
| flush | () | method |
public static function flush(c:Class = null):voidUse this method delete stored instances and free some memory
Parametersc:Class (default = null) — The Class whose stored instances are to be flushed. Pass nothing or null to flush them all.
|
| getInstanceOf | () | method |
public static function getInstanceOf(c:Class):ObjectThis method returns an instance of a given Class. If some is available to be reused, that one is used. Otherwise a new instance will be created.
Parametersc:Class — The Class that is to be instantiated
|
Object — An instance of the given class. You will need to cast it into the appropiate type
|
| returnInstance | () | method |
public static function returnInstance(object:Object):voidUse this method to return unused objects to the pool and make them available to be used again later. Make sure you remove old references to this object or you will get all sorts of weird results.
For convenience, if the instance is a DisplayObject, its coordinates, transform values, filters, etc. are reset.
Parametersobject:Object — The object you are returning to the pool
|