new ObjectPool(name, generator, resetFunc, destroyFuncnullable, maxStore, behaviorOnOverflow, poolSelectionopt, doUpdateopt, doDrawopt, clickableopt)
An object pool that handles pooling automatically. Useful for particle systems
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
name |
String | name of ObjectPool | ||
generator |
generatorCallback | |||
resetFunc |
resetFuncCallback | |||
destroyFunc |
destroyFuncCallback |
<nullable> |
||
maxStore |
Number | the maximum amount of objects that can be stored in the pool | ||
behaviorOnOverflow |
POOL_BEHAVIOR_ON_OVERFLOW | how the pool should react when too many objects are spawned | ||
poolSelection |
POOL_SELECTION |
<optional> |
POOL_SELECTION.FIRST | how unallocated objects should be selected from the pool |
doUpdate |
Boolean |
<optional> |
true | whether or not to update the objects in the pool |
doDraw |
Boolean |
<optional> |
true | whether or not to draw the items in the pool |
clickable |
Boolean |
<optional> |
false | Whether or not the items in the pool are clickable |
Properties:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
objectConstructor |
generatorCallback | the generator for new objects | ||
resetFunc |
resetFuncCallback | the function to apply to all spawned objects | ||
destroyFunc |
destroyFuncCallback |
<nullable> |
the function to apply to all objects being destroyed, in case any cleanup is necessary | |
maxStore |
Number | the maximum pool size | ||
pooled |
Array | the list of pooled objects, both allocated and unallocated | ||
children |
Array | the allocated objects in the pool (i.e. only spawned ones) | ||
select |
POOL_SELECTION | the selection method for allocation | ||
bof |
POOL_BEHAVIOR_ON_OVERFLOW | the pool behavior on overflow | ||
_isAggregate |
Boolean | true | true to indicate the object represents an aggregate of other objects |
- Source:
Extends
Members
(readonly) length
Properties:
| Name | Type | Description |
|---|---|---|
length |
Number | length of SceneGraph |
- Inherited From:
- Source:
Methods
byName(name) → {Array}
Gets all elements in a SceneGraph with a certain name
Parameters:
| Name | Type | Description |
|---|---|---|
name |
String | name to search for |
- Inherited From:
- Source:
Returns:
all children with name
- Type
- Array
constructor(name, generator, resetFunc, destroyFuncnullable, maxStore, behaviorOnOverflow, poolSelectionopt, doUpdateopt, doDrawopt, clickableopt)
constructor
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
name |
String | name of ObjectPool | ||
generator |
generatorCallback | |||
resetFunc |
resetFuncCallback | |||
destroyFunc |
destroyFuncCallback |
<nullable> |
||
maxStore |
Number | the maximum amount of objects that can be stored in the pool | ||
behaviorOnOverflow |
POOL_BEHAVIOR_ON_OVERFLOW | how the pool should react when too many objects are spawned | ||
poolSelection |
POOL_SELECTION |
<optional> |
POOL_SELECTION.FIRST | how unallocated objects should be selected from the pool |
doUpdate |
Boolean |
<optional> |
true | whether or not to update the objects in the pool |
doDraw |
Boolean |
<optional> |
true | whether or not to draw the items in the pool |
clickable |
Boolean |
<optional> |
false | Whether or not the items in the pool are clickable |
- Overrides:
- Source:
draw(context)
Handles draws by drawing children
Parameters:
| Name | Type | Description |
|---|---|---|
context |
RenderingContext | context to draw on |
- Inherited From:
- Source:
filter(func) → {Array}
Filters the SceneGraph. Proxy for SceneGraph.children.filter
Parameters:
| Name | Type | Description |
|---|---|---|
func |
function | filter to use |
- Inherited From:
- Source:
Returns:
filtered data
- Type
- Array
first() → {object}
Gets the first object in the SceneGraph
- Inherited From:
- Source:
Returns:
first object
- Type
- object
firstByName(name) → {object}
Gets first element in a SceneGraph with a certain name
Parameters:
| Name | Type | Description |
|---|---|---|
name |
String | name to search for |
- Inherited From:
- Source:
Returns:
first child with name
- Type
- object
forEach(func)
Runs a foreach query on the scene graph. Proxy for SceneGraph.children.forEach(func)
Parameters:
| Name | Type | Description |
|---|---|---|
func |
function | the function to run |
- Inherited From:
- Source:
forEachReturn(func) → {Array.<object>}
Runs a foreach query on the scene graph and returns the results.
Parameters:
| Name | Type | Description |
|---|---|---|
func |
function | the function to run |
- Inherited From:
- Source:
Returns:
outputs of forEach
- Type
- Array.<object>
forEachReverse(func)
Runs a foreach query on the scene graph in reverse.
Parameters:
| Name | Type | Description |
|---|---|---|
func |
function | the function to run |
- Inherited From:
- Source:
- See:
forEachReverseReturn(func) → {Array.<object>}
Runs a foreach query on the scene graph in reverse and returns the results.
Parameters:
| Name | Type | Description |
|---|---|---|
func |
function | the function to run |
- Inherited From:
- Source:
- See:
Returns:
outputs of forEach
- Type
- Array.<object>
forEachReverseUntilFirstSuccess(func, deepCheckopt) → {Boolean|object}
Runs a foreach query on the scene graph in reverse until a child returns true.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
func |
function | the function to run | ||
deepCheck |
Boolean |
<optional> |
false | if true, will expand all child scene graphs when running query |
- Inherited From:
- Source:
- See:
Returns:
object which returned true
- Type
- Boolean | object
forEachUntilFirstSuccess(func, deepCheckopt) → {Boolean|object}
Runs a foreach query on the scene graph until a child returns true.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
func |
function | the function to run | ||
deepCheck |
Boolean |
<optional> |
false | if true, will expand all child scene graphs when running query |
- Inherited From:
- Source:
Returns:
object which returned true; if no objects return true, returns false
- Type
- Boolean | object
indexOf(e) → {Number}
finds the index of an object in the scene graph
Parameters:
| Name | Type | Description |
|---|---|---|
e |
object | the object to find |
- Inherited From:
- Source:
Returns:
the index of the object, -1 if not found
- Type
- Number
isEmpty()
Returns true if the SceneGraph is empty and contains no objects
- Inherited From:
- Source:
Returns:
true if SceneGraph is empty
last() → {object}
Gets the last object in the SceneGraph
- Inherited From:
- Source:
Returns:
last object
- Type
- object
lastByName(name) → {object}
Gets last element in a SceneGraph with a certain name
Parameters:
| Name | Type | Description |
|---|---|---|
name |
String | name to search for |
- Inherited From:
- Source:
Returns:
last child with name
- Type
- object
mouseDown(game, event, returnOnFirstSuccessopt) → {object|Array.<object>}
handles mouseDown by passing event along to children if clickable
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
game |
Game | instance of the game | ||
event |
MouseEvent | instance of the mouse event | ||
returnOnFirstSuccess |
Boolean |
<optional> |
true | whether or not to stop once we've found one object that is clicked |
- Inherited From:
- Source:
Returns:
output of mouseDown. Will likely be undefined
- Type
- object | Array.<object>
mouseUp(game, event, returnOnFirstSuccessopt) → {object|Array.<object>}
handles mouseUp by passing event along to children if clickable
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
game |
Game | instance of the game | ||
event |
MouseEvent | instance of the mouse event | ||
returnOnFirstSuccess |
Boolean |
<optional> |
true | whether or not to stop once we've found one object that is clicked |
- Inherited From:
- Source:
Returns:
output of mouseDown. Will likely be undefined
- Type
- object | Array.<object>
moveToBack(index)
Moves an object to the back of the SceneGraph
Parameters:
| Name | Type | Description |
|---|---|---|
index |
Number | index to move |
- Inherited From:
- Source:
moveToFront(index)
Moves an object to the front of the SceneGraph
Parameters:
| Name | Type | Description |
|---|---|---|
index |
Number | index to move |
- Inherited From:
- Source:
newInstance() → {PooledInstance}
Returns new instance of pooled object
- Source:
Returns:
new instance of allocated object
- Type
- PooledInstance
pointCollide(x, y, limitToClickable) → {Array}
Checks for collisions at a certain point with objects contained in the scene graph. Will search through any SceneGraphs that are a child of this one as well.
Please see the example for how output is handled. You may want to run output through flatten
Parameters:
| Name | Type | Description |
|---|---|---|
x |
Number | the x value to check |
y |
Number | the y value to check |
limitToClickable |
Boolean | whether or not to limit the search to clickable objects only |
- Inherited From:
- Source:
Returns:
n-dimensional list of which objects collide.
- Type
- Array
Example
// sg is an already initalized SceneGraph
// sg array representation == [objectA, objectB, [objectC, [objectD, objectE]]] (where inner arrays are other SceneGraphs)
// Assume objectA and objectC don't collide at (0, 0), but the others do
console.log(sg.pointCollide(0,0,false)); // [undefined, objectB, [undefined, [objectD, objectE]]]
console.log(flatten(sg.pointCollide(0,0,false))); // [objectB, objectD, objectE]
pop() → {object}
pops a new object from the SceneGraph
- Inherited From:
- Source:
Returns:
popped object
- Type
- object
push(object) → {object}
pushes a new object onto the SceneGraph
Parameters:
| Name | Type | Description |
|---|---|---|
object |
object | to add |
- Inherited From:
- Source:
Returns:
added object
- Type
- object
recycle(pooledObject)
Recycles a pooled instance from the pool
Parameters:
| Name | Type | Description |
|---|---|---|
pooledObject |
PooledInstance | thing to recycle |
- Source:
remove(e) → {GameObject}
Recycles/removes an object
Parameters:
| Name | Type | Description |
|---|---|---|
e |
GameObject | object to remove |
- Source:
Returns:
removed object
- Type
- GameObject
removeAll()
Recycles all objects in pool
- Overrides:
- Source:
removeIndex(index) → {object}
removes the object at a certain index into the scene graph
Parameters:
| Name | Type | Description |
|---|---|---|
index |
Number | the index to remove |
- Overrides:
- Source:
Returns:
the removed object
- Type
- object
shift() → {object}
shifts a new object from the SceneGraph (removes from start)
- Inherited From:
- Source:
Returns:
unshifted object
- Type
- object
spawn() → (nullable) {PooledInstance}
Spawns a new object and adds it to the pool
- Source:
Returns:
null if overflow and rules set to not allow overflow, else spawned object
- Type
- PooledInstance
spawnSeveral(n) → {Array.<PooledInstance>}
Spawns multiple objects
Parameters:
| Name | Type | Description |
|---|---|---|
n |
Number | how many objects to spawn |
- Source:
Returns:
list of spawned objects
- Type
- Array.<PooledInstance>
unshift(object) → {object}
unshifts a new object to the SceneGraph (adds to start)
Parameters:
| Name | Type | Description |
|---|---|---|
object |
object | to add |
- Inherited From:
- Source:
Returns:
added object
- Type
- object
update(game)
Handles updates by updating children
Parameters:
| Name | Type | Description |
|---|---|---|
game |
Game | instance of game |
- Inherited From:
- Source:
updatePosition()
Handles updates of positions by updating positions of children (new positions are stored and then updated after all other update stuff, e.g. collisions, is handled)
- Inherited From:
- Source: