Kiwi.Group Class
The group class is central to creating the scene graph that contains all objects in a state. A group can contain entities or other groups, thereby enabling a nested tree scene graph. The members of the Group's coordinates are also in relation to the Group that they were added to. So if you moved an entire Group, each member of that Group would also 'move'.
Constructor
Kiwi.Group
-
state
-
[name='']
Parameters:
-
state
Kiwi.StateThe State that this Group is a part of.
-
[name='']
String optionalThe name of this group.
Returns:
Item Index
Methods
- addChild
- addChildAfter
- addChildAt
- addChildBefore
- callAll
- clear
- contains
- containsAncestor
- containsDescendant
- countDead
- countLiving
- destroy
- forEach
- forEachAlive
- getChildAt
- getChildByID
- getChildByName
- getChildIndex
- getFirstAlive
- getFirstDead
- numChildren
- objType
- removeChild
- removeChildAt
- removeChildren
- removeFirstAlive
- render
- replaceChild
- setAll
- setChildIndex
- swapChildren
- swapChildrenAt
- update
Methods
addChild
-
child
Adds an Entity to this Group. The Entity must not already be in this Group.
Parameters:
-
child
ObjectThe child to be added.
Returns:
The child that was added.
addChildAfter
-
child
-
beforeChild
Adds an Entity to this Group after another child. The Entity must not already be in this Group and it must be supported by the Group..
Parameters:
-
child
ObjectThe child to be added.
-
beforeChild
ObjectThe child after which the child will be added.
Returns:
The child.
addChildAt
-
child
-
index
Adds an Entity to this Group in the specific location. The Entity must not already be in this Group and it must be supported by the Group.
Parameters:
-
child
ObjectThe child to be added.
-
index
NumberThe index the child will be set at.
Returns:
The child.
addChildBefore
-
child
-
beforeChild
Adds an Entity to this Group before another child. The Entity must not already be in this Group and it must be supported by the Group.
Parameters:
-
child
ObjectThe child to be added.
-
beforeChild
EntityThe child before which the child will be added.
Returns:
The child.
callAll
-
componentName
-
functionName
-
args
Calls a function on every member. If componentName is null the function is called on the entity itself, otherwise it is called on the named component. Uses runtime string property lookups. Not optimal for large groups if speed is an issue.
Parameters:
-
componentName
StringThe name of the component to call the function on - set to null to call a function on the entity.
-
functionName
StringThe name of the function to call.
-
args
ArrayAn array of arguments to pas to the function.
clear
()
public
Clear all children from this Group
contains
-
child
Checks if the given entity is in this group
Parameters:
-
child
IChildThe IChild that you want to checked.
Returns:
true if entity exists in group.
containsAncestor
-
descendant
-
ancestor
Checks to see if one child is an ansector of another child.
Parameters:
-
descendant
ObjectThe object that you are checking.
-
ancestor
GroupThe parent (ancestor) that you are checking for.
Returns:
containsDescendant
-
child
Checks to see if the given object is contained in this group as a descendant
Parameters:
-
child
ObjectThe IChild that you want to check.
Returns:
countDead
()
Number
public
Returns the number of member which are not marked as 'alive'
Returns:
countLiving
()
Number
public
Returns the number of member which are marked as 'alive'
Returns:
destroy
-
[immediate=false]
-
[destroyChildren=true]
Removes all children and destroys the Group.
Parameters:
-
[immediate=false]
Boolean optionalIf the object should be immediately removed or if it should be removed at the end of the next update loop.
-
[destroyChildren=true]
Boolean optionalIf all of the children on the group should also have their destroy methods called.
forEach
-
context
-
callback
-
[params]
Loops through each member in the group and run a method on for each one.
Parameters:
-
context
AnyThe context that the callbacks are to have when called.
-
callback
AnyThe callback method to execute on each member.
-
[params]
Any optional multipleAny extra parameters.
forEachAlive
-
context
-
callback
-
[params]
Loop through each member of the groups that is alive.
Parameters:
-
context
AnyThe context that the callbacks are to have when called.
-
callback
AnyThe callback method to execute on each member.
-
[params]
Any optional multipleAny extra parameters.
getChildAt
-
index
Get the child at a specific position in this Group by its index.
Parameters:
-
index
NumberThe index of the child
Returns:
The child, if found or null if not.
getChildByID
-
id
Get a child from this Group by its UUID.
Parameters:
-
id
StringThe ID of the child.
Returns:
The child, if found or null if not.
getChildByName
-
name
Get a child from this Group by its name.
Parameters:
-
name
StringThe name of the child
Returns:
The child, if found or null if not.
getChildIndex
-
child
Returns the index position of the Entity or -1 if not found.
Parameters:
-
child
ObjectThe child.
Returns:
The index of the child or -1 if not found.
getFirstAlive
()
Object
public
Returns the first Entity from this Group marked as 'alive' or null if no members are alive
Returns:
getFirstDead
()
Object
public
Returns the first member of the Group which is not 'alive', returns null if all members are alive.
Returns:
numChildren
()
Number
public
Returns the total number of children in this Group. Doesn't distinguish between alive and dead children.
Returns:
The number of children in this Group
objType
()
String
public
Returns the type of this object
Returns:
The type of this object
removeChild
-
child
-
[destroy=false]
Removes an Entity from this Group if it is a child of it.
Parameters:
-
child
ObjectThe child to be removed.
-
[destroy=false]
Boolean optionalIf the entity that gets removed should be destroyed as well.
Returns:
The child.
removeChildAt
-
index
Removes the Entity from this Group at the given position.
Parameters:
-
index
NumberThe index of the child to be removed.
Returns:
The child, or null.
removeChildren
-
begin
-
end
-
destroy
Removes all Entities from this Group within the given range.
Parameters:
-
begin
NumberThe begining index.
-
end
NumberThe last index of the range.
-
destroy
NumberIf the children should be destroyed as well.
Returns:
The number of removed entities.
removeFirstAlive
-
[destroy=false]
Removes the first Entity from this Group marked as 'alive'
Parameters:
-
[destroy=false]
Boolean optionalIf the entity should run the destroy method when it is removed.
Returns:
The Entity that was removed from this Group if alive, otherwise null
render
-
camera
The render method that is required by the IChild. This method never gets called as the render is only worried about rendering entities.
Parameters:
-
camera
Kiwi.Camera
replaceChild
-
oldChild
-
newChild
Replaces a child Entity in this Group with a new one.
Parameters:
-
oldChild
ObjectThe Entity in this Group to be removed.
-
newChild
ObjectThe new Entity to insert into this Group at the old Entities position.
Returns:
true if the Entities were replaced successfully, otherwise false.
setAll
-
componentName
-
property
-
value
Sets a property on every member. If componentName is null the property is set on the entity itself, otherwise it is set on the named component. Uses runtime string property lookups. Not optimal for large groups if speed is an issue.
Parameters:
-
componentName
StringThe name of the component to set the property on - set to null to set a property on the entity.
-
property
StringThe name of the property to set.
-
value
AnyThe value to set the property to.
setChildIndex
-
child
-
index
Sets a new position of an existing Entity within the Group.
Parameters:
-
child
ObjectThe child in this Group to change.
-
index
NumberThe index for the child to be set at.
Returns:
true if the Entity was moved to the new position, otherwise false.
swapChildren
-
child1
-
child2
Swaps the position of two existing Entities that are a direct child of this group.
Parameters:
-
child1
ObjectThe first child in this Group to swap.
-
child2
ObjectThe second child in this Group to swap.
Returns:
true if the Entities were swapped successfully, otherwise false.
swapChildrenAt
-
index1
-
index2
Swaps the position of two existing Entities within the Group based on their index.
Parameters:
-
index1
NumberThe position of the first Entity in this Group to swap.
-
index2
NumberThe position of the second Entity in this Group to swap.
Returns:
true if the Entities were swapped successfully, otherwise false.
update
()
public
The update loop for this group.
Properties
_active
Boolean
private
An active Entity is one that has its update method called by its parent.
Default: true
_destroyRemoveChildren
Boolean
private
A temporary property that holds a boolean indicating whether or not the group's children should be destroyed or not.
_dirty
Boolean
private
An indication of whether or not this group is 'dirty' and thus needs to be re-rendered or not.
_willRender
Boolean
private
Controls whether render is automatically called by the parent.
active
Boolean
public
Toggles the active state of this Entity. An Entity that is active has its update method called by its parent. This method should be over-ridden to handle specific dom/canvas/webgl implementations.
Default: true
dirty
Boolean
public
Sets all children of the Group to be dirty.
exists
Boolean
private
If an Entity no longer exists it is cleared for garbage collection or pool re-allocation
exists
Boolean
public
Toggles the exitence of this Group. An Entity that no longer exists can be garbage collected or re-allocated in a pool This method should be over-ridden to handle specific canvas/webgl implementations.
id
String
public
A unique identifier for this Group within the game used internally by the framework. See the name property for a friendly version.
members
Array
public
The collection of children belonging to this group
name
String
public
A name for this Group. This is not checked for uniqueness within the Game, but is very useful for debugging.
Default: ''
parent
Kiwi.Group
public
Set's the parent of this entity. Note that this also sets the transforms parent of this entity to be the passed groups transform.
transform
Kiwi.Geom.Transform
public
The transform object for this group. Transform handles the calculation of coordinates/rotation/scale e.t.c in the Game World.
willRender
Boolean
public
Controls whether render is automatically caleld by the parent.
x
Number
public
The X coordinate of this group. This is just aliased to the transform property.
y
Number
public
The Y coordinate of this group. This is just aliased to the transform property.