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 add or remove sprites.
Type:
-
Number
velocity :p5.Vector
The sprite's velocity vector {x, y}
Type:
-
p5.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}
Remove 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 removed | |
bottom |
Number
|
the distance that sprites can move below the canvas before they are removed | |
left |
Number
|
the distance that sprites can move beyond the left side of the canvas before they are removed | |
right |
Number
|
the distance that sprites can move beyond the right side of the canvas before they are removed | |
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 removed |
Returns:
- Type:
-
Number
the number of sprites culled
Example
// alternate uniform size syntax
group.cull(100, callback);
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.
push(…sprites) → {Number}
Its 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.
Adds a sprite or multiple sprites to the group, whether they were
already in the group or not, just like with the Array.push()
method. Only sprites can be added to a 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
remove(item) → {Sprite}
If no input is given to this function, the group itself will be
marked as removed and deleted from p5play's internal memory, the
`p5play.groups` array. Every sprite in the group will be removed
from the world and every other group they belong to.
Groups should not be used after they are removed.
If this function receives as input an index of a sprite in the
group or the sprite itself, it will remove that sprite from
this group and its super groups (if any), but NOT from the world.
To remove a sprite from the world and every group it belongs to,
use `sprite.remove()` instead.
Parameters:
Name | Type | Description |
---|---|---|
item |
Sprite
|
Number
|
the sprite to be removed or its index |
removeAll()
Removes all the sprites in the group from the world and
every other group they belong to.
Does not delete the group itself.
shift() → {Sprite}
Removes the last sprite in the group.
splice(idx, amount) → {Array:.<Sprite:>}
Using `group.remove` instead is recommended because it's easier to use,
and it uses this function internally.
Similar to `Array.splice` except it does not accept adding sprites,
third parameters and beyond are ignored.
This function also removes the group and its super-groups from the
sprites' groups array.
Parameters:
Name | Type | Description |
---|---|---|
idx |
Number
|
index |
amount |
Number
|
number of sprites to remove |
toString() → {String}
Returns the group's unique identifier.
Returns:
- Type:
-
String
groupID
unshift() → {Number}
Not supported!
Returns:
- Type:
-
Number
the new length of the group