new Group()
Look at the Group reference pages before reading these docs.
A Group is an array of sprites with similar traits and behaviors.
Group extends Array, so you can use them in for of loops. They
inherit all the functions and properties of standard arrays
such as `group.length` and functions like `group.includes()`.
Changing a group setting changes it for all the sprites in the
group, similar to class inheritance. Groups can have subgroups,
creating a hierarchy of inheritance.
The top level group is a q5 instance level variable named
`allSprites` that contains all the sprites added to the sketch.
Extends
- Array:.<Sprite:>
Classes
Members
amount :Number
Depending on the value that the amount property is set to, the group will
either automatically create or delete sprites.
Type:
-
Number
velocity :Vector
The sprite's velocity vector {x, y}
Type:
-
Vector
Methods
collided(target, callbackopt) → {Boolean}
Returns true on the first frame that the group no longer overlaps
with the target group.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
target |
Group
|
||
callback |
function
|
<optional> |
Returns:
- Type:
-
Boolean
collides(target, callbackopt)
Returns true on the first frame that the group collides with the
target group.
Custom collision event handling can be done by using this function
in an if statement or adding a callback as the second parameter.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
target |
Group
|
||
callback |
function
|
<optional> |
colliding(target, callbackopt) → {Number}
Returns the amount of frames that the group has been colliding
with the target group for, which is a truthy value. Returns 0 if
the group is not colliding with the target group.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
target |
Group
|
||
callback |
function
|
<optional> |
Returns:
- Type:
-
Number
frames
cull(top, bottom, left, right, cbopt) → {Number}
Delete sprites that go outside the given culling boundary
relative to the camera.
Sprites with chain colliders can not be culled.
Can also be used with a uniform size for all boundaries, see example.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
top |
Number
|
the distance that sprites can move below the canvas before they are deleted | |
bottom |
Number
|
the distance that sprites can move below the canvas before they are deleted | |
left |
Number
|
the distance that sprites can move beyond the left side of the canvas before they are deleted | |
right |
Number
|
the distance that sprites can move beyond the right side of the canvas before they are deleted | |
cb |
function
|
<optional> |
the function to be run when a sprite is culled, it's given the sprite being culled, if no callback is given then the sprite is deleted |
Returns:
- Type:
-
Number
the number of sprites culled
Example
// alternate uniform size syntax
group.cull(100, callback);
delete()
Deletes the group and all its sprites
from the world and every other group they belong to.
Don't attempt to use a group after deleting it.
deleteAll()
Deletes all the sprites in the group
from the world and every other group they belong to.
Does not delete the group itself.
draw()
Draws all the sprites in the group in ascending order
by `sprite.layer`.
overlapped(target, callbackopt) → {Boolean}
Returns true on the first frame that the group no longer overlaps
with the target group.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
target |
Group
|
||
callback |
function
|
<optional> |
Returns:
- Type:
-
Boolean
overlapping(target, callbackopt) → {Number}
Returns the amount of frames that the group has been overlapping
with the target group for, which is a truthy value. Returns 0 if
the group is not overlapping with the target group.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
target |
Group
|
||
callback |
function
|
<optional> |
Returns:
- Type:
-
Number
frames
overlaps(target, callbackopt)
Returns true on the first frame that the group overlaps with the
target group.
Custom overlap event handling can be done by using this function
in an if statement or adding a callback as the second parameter.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
target |
Group
|
||
callback |
function
|
<optional> |
pop() → {Sprite}
Removes the last sprite in the group from the group.
push(…sprites) → {Number}
Adds a sprite or multiple sprites to the group at the end of
the group list, just like the `Array.push()` method.
Instead of creating a sprite and adding it to a group with this
method, it's better to use the group Sprite constructor instead,
`new group.Sprite()`, which both creates a group sprite using
soft inheritance and adds it to the group list.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
sprites |
Sprite
|
<repeatable> |
The sprite or sprites to be added |
Returns:
- Type:
-
Number
the new length of the group
remove(item) → {Sprite}
Removes a sprite from this group and its sub groups (if any),
but does not delete it from the world.
Parameters:
Name | Type | Description |
---|---|---|
item |
Sprite
|
Number
|
the sprite to be deleted or its index |
shift() → {Sprite}
Removes the last sprite in the group from the group.
splice(start, removalCount, …sprites) → {Array:.<Sprite:>}
If removalCount is greater than 0, that amount of
sprites starting from the start index will be removed
from this group and its sub groups recursively (if any),
Then any provided sprites will be added at the start index
to this group and at the end of each of its parent groups recursively,
if not already present in parent groups.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
start |
Number
|
start index | |
removalCount |
Number
|
number of sprites to remove, starting from the start index | |
sprites |
Sprite
|
<repeatable> |
sprites to add at start index |
toString() → {String}
Returns the group's unique identifier.
Returns:
- Type:
-
String
groupID
unshift(…sprites) → {Number}
Adds one or more sprites to the beginning of the group.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
sprites |
Sprite
|
<repeatable> |
the sprite or sprites to be added |
Returns:
- Type:
-
Number
the new length of the group