new Group()
Look at the Group reference pages before reading these docs.
A Group is a collection of sprites with similar traits and behaviors.
For example a group may contain all the coin sprites that the
player can collect.
Group extends Array. You can use them in for loops just like arrays
since they inherit all the functions and properties of standard
arrays such as group.length and function like group.includes().
Since groups just contain references to sprites, a sprite can be in
multiple groups.
`sprite.remove()` removes the sprite from all the groups
it belongs to. `group.removeAll()` removes all the sprites from
a group.
The top level group is a p5 instance level variable named
`allSprites` that contains all the sprites added to the sketch.
Members
amount :Number
Depending on the value that the amount property is set to, the group will
either add or remove sprites.
Type:
-
Number
Methods
applyForce(forceVector, forceOriginopt)
Apply a force that is scaled to the sprite's mass.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
forceVector |
p5.Vector
|
Array
|
force vector | |
forceOrigin |
p5.Vector
|
Array
|
<optional> |
force origin |
collided(target, callbackopt) → {Boolean}
Returns true on the first frame that the group no longer overlaps
with the target sprite or group.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
target |
Sprite
|
Group
|
||
callback |
function
|
<optional> |
Returns:
- Type:
-
Boolean
collides(target, callbackopt)
Returns true on the first frame that the group collides with the
target sprite or 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 |
Sprite
|
Group
|
||
callback |
function
|
<optional> |
colliding(target, callbackopt) → {Number}
Returns a truthy value while the group is colliding with the
target sprite or group. The value is the number of frames that
the group has been colliding with the target.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
target |
Sprite
|
Group
|
||
callback |
function
|
<optional> |
Returns:
- Type:
-
Number
frames
cull(top|size, bottom|cb, leftopt, rightopt, cb(sprite)opt) → {Number}
Remove sprites that go outside the given culling boundary
relative to the camera.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
top|size |
Number
|
The distance that sprites can move below the p5.js canvas before they are removed. *OR* The distance sprites can travel outside the screen on all sides before they get removed. | |
bottom|cb |
Number
|
The distance that sprites can move below the p5.js canvas before they are removed. | |
left |
Number
|
<optional> |
The distance that sprites can move beyond the left side of the p5.js canvas before they are removed. |
right |
Number
|
<optional> |
The distance that sprites can move beyond the right side of the p5.js canvas before they are removed. |
cb(sprite) |
function
|
<optional> |
The callback is given the sprite that passed the cull boundary, if no callback is given the sprite is removed by default |
Returns:
- Type:
-
Number
The number of sprites culled
get(i)
Gets the member at index i.
Parameters:
Name | Type | Description |
---|---|---|
i |
Number
|
The index of the object to retrieve |
orbit(amount)
EXPERIMENTAL! Subject to change in the future!
Rotates the group around its centroid.
Parameters:
Name | Type | Description |
---|---|---|
amount |
Number
|
Amount of rotation |
overlapped(target, callbackopt) → {Boolean}
Returns true on the first frame that the group no longer overlaps
with the target sprite or group.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
target |
Sprite
|
Group
|
||
callback |
function
|
<optional> |
Returns:
- Type:
-
Boolean
overlapping(target, callbackopt) → {Number}
Returns a truthy value while the group is overlapping with the
target sprite or group. The value returned is the number of
frames the group has been overlapping with the target.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
target |
Sprite
|
Group
|
||
callback |
function
|
<optional> |
Returns:
- Type:
-
Number
frames
overlaps(target, callbackopt)
Returns true on the first frame that the group overlaps with the
target sprite or 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 |
Sprite
|
Group
|
||
callback |
function
|
<optional> |
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 all sprites in the group are removed.
If a sprite or index is given, that sprite is removed from the
group, but not from the sketch or any other groups it may be in.
Parameters:
Name | Type | Description |
---|---|---|
item |
Sprite
|
The sprite to be removed |
toString() → {String}
Returns the group's unique identifier.
Returns:
- Type:
-
String
groupID