new SceneGraph(name, doUpdateopt, doDrawopt, clickableopt, updateDirectionopt, drawDirectionopt)
A SceneGraph holds all of the objects in the game
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
name |
String | the name of the SceneGraph | ||
doUpdate |
Boolean |
<optional> |
true | whether or not the scene graph should perform updates |
doDraw |
Boolean |
<optional> |
true | whether or not the scene graph should do draw calls |
clickable |
Boolean |
<optional> |
true | whether or not the objects in this scene graph are clickable |
updateDirection |
String |
<optional> |
"forwards" | which direction to parse tree in when updating. "forwards" or "backwards" |
drawDirection |
String |
<optional> |
"backwards" | which direction to parse tree in when drawing. "forwards" or "backwards" |
Properties:
| Name | Type | Default | Description |
|---|---|---|---|
name |
String | the name of the SceneGraph | |
doUpdate |
Boolean | the value of if this SceneGraph should update. | |
doDraw |
Boolean | the value of if objects in this SceneGraph should be drawn. | |
isClickable |
Boolean | the value of if the objects in the SceneGraph should be clickable. | |
children |
Array | the array that holds all the objects | |
updateDirection |
String | the direction that the tree will be navigated to update objects. This value can only be either "forwards" or "backwards". | |
drawDirection |
String | the direction that the tree will be navigated to draw objects. This value can only be either "forwards" or "backwards" | |
isSceneGraph |
Boolean | true | true to indicate the object is a SceneGraph |
_isAggregate |
Boolean | true | true to indicate the object represents an aggregate of other objects |
- Source:
Members
(readonly) length
Properties:
| Name | Type | Description |
|---|---|---|
length |
Number | length of SceneGraph |
- 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 |
- Source:
Returns:
all children with name
- Type
- Array
constructor(name, doUpdateopt, doDrawopt, clickableopt, updateDirectionopt, drawDirectionopt)
Creates the SceneGraph objects
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
name |
String | the name of the SceneGraph | ||
doUpdate |
Boolean |
<optional> |
true | whether or not the scene graph should perform updates |
doDraw |
Boolean |
<optional> |
true | whether or not the scene graph should do draw calls |
clickable |
Boolean |
<optional> |
true | whether or not the objects in this scene graph are clickable |
updateDirection |
String |
<optional> |
"forwards" | which direction to parse tree in when updating. "forwards" or "backwards" |
drawDirection |
String |
<optional> |
"backwards" | which direction to parse tree in when drawing. "forwards" or "backwards" |
- Source:
draw(context)
Handles draws by drawing children
Parameters:
| Name | Type | Description |
|---|---|---|
context |
RenderingContext | context to draw on |
- Source:
filter(func) → {Array}
Filters the SceneGraph. Proxy for SceneGraph.children.filter
Parameters:
| Name | Type | Description |
|---|---|---|
func |
function | filter to use |
- Source:
Returns:
filtered data
- Type
- Array
first() → {object}
Gets the first object in the SceneGraph
- 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 |
- 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 |
- 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 |
- 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 |
- 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 |
- 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 |
- 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 |
- 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 |
- 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
- Source:
Returns:
true if SceneGraph is empty
last() → {object}
Gets the last object in the SceneGraph
- 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 |
- 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 |
- 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 |
- 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 |
- Source:
moveToFront(index)
Moves an object to the front of the SceneGraph
Parameters:
| Name | Type | Description |
|---|---|---|
index |
Number | index to move |
- Source:
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 |
- 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
- Source:
Returns:
popped object
- Type
- object
push(object) → {object}
pushes a new object onto the SceneGraph
Parameters:
| Name | Type | Description |
|---|---|---|
object |
object | to add |
- Source:
Returns:
added object
- Type
- object
removeAll()
removes all objects from the scene graph
- 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 |
- Source:
Returns:
the removed object
- Type
- object
removeIndex(e) → {object}
removes the given object from the scene graph
Parameters:
| Name | Type | Description |
|---|---|---|
e |
object | the object to remove |
- Source:
Returns:
the removed object
- Type
- object
shift() → {object}
shifts a new object from the SceneGraph (removes from start)
- Source:
Returns:
unshifted object
- Type
- object
unshift(object) → {object}
unshifts a new object to the SceneGraph (adds to start)
Parameters:
| Name | Type | Description |
|---|---|---|
object |
object | to add |
- Source:
Returns:
added object
- Type
- object
update(game)
Handles updates by updating children
Parameters:
| Name | Type | Description |
|---|---|---|
game |
Game | instance of game |
- 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)
- Source: