Group
In p5.play groups are collections of sprites with similar behavior. For example a group may contain all the sprites in the background or all the sprites that "kill" the player.
Groups are "extended" arrays and inherit all their properties e.g. group.length
Since groups contain only references, a sprite can be in multiple groups and deleting a group doesn't affect the sprites themselves.
Sprite.remove() will also remove the sprite from all the groups it belongs to.
Table of Contents
-
Methods
- _groupCollide(typetargetcallback)
- add(s)
- bounce(targetcallback)
- bounceOff(targetcallback)
- clear()
- collide(targetcallback)
- contains(sprite)
- createEdgeSprites()
- displace(targetcallback)
- draw()
- get(i)
- indexOf()
- isTouching(target)
- maxDepth()
- minDepth()
- overlap(targetcallback)
- remove(item)
- removeSprites()
- size()
- toArray()
Constructor
Group
()
Methods
_groupCollide
-
type
-
target
-
callback
Collide each member of group against the target using the given collision type. Return true if any collision occurred. Internal use
Parameters:
-
type
!stringone of 'overlap', 'collide', 'displace', 'bounce' or 'bounceOff'
-
target
ObjectGroup or Sprite
-
[callback]
Function optionalon collision.
Returns:
True if any collision/overlap occurred
bounce
-
target
-
callback
Checks if the the group is overlapping another group or sprite. If the overlap is positive the sprites will bounce affecting each other's trajectories depending on their .velocity, .mass and .restitution.
The check is performed using the colliders. If colliders are not set they will be created automatically from the image/animation bounding box.
A callback function can be specified to perform additional operations when the overlap occours. The function will be called for each single sprite overlapping. The parameter of the function are respectively the member of the current group and the other sprite passed as parameter.
Parameters:
-
target
ObjectGroup or Sprite to check against the current one
-
[callback]
Function optionalThe function to be called if overlap is positive
Returns:
True if overlapping
Example:
group.bounce(otherSprite, explosion);
function explosion(spriteA, spriteB) {
spriteA.remove();
spriteB.score++;
}
bounceOff
-
target
-
callback
Checks if the the group is overlapping another group or sprite. If the overlap is positive the sprites will bounce with the target(s) treated as immovable.
The check is performed using the colliders. If colliders are not set they will be created automatically from the image/animation bounding box.
A callback function can be specified to perform additional operations when the overlap occours. The function will be called for each single sprite overlapping. The parameter of the function are respectively the member of the current group and the other sprite passed as parameter.
Parameters:
-
target
ObjectGroup or Sprite to check against the current one
-
[callback]
Function optionalThe function to be called if overlap is positive
Returns:
True if overlapping
Example:
group.bounceOff(otherSprite, explosion);
function explosion(spriteA, spriteB) {
spriteA.remove();
spriteB.score++;
}
clear
()
Removes all references to the group. Does not remove the actual sprites.
collide
-
target
-
callback
Checks if the the group is overlapping another group or sprite. If the overlap is positive the sprites will bounce with the target(s) treated as immovable with a restitution coefficient of zero.
The check is performed using the colliders. If colliders are not set they will be created automatically from the image/animation bounding box.
A callback function can be specified to perform additional operations when the overlap occours. The function will be called for each single sprite overlapping. The parameter of the function are respectively the member of the current group and the other sprite passed as parameter.
Parameters:
-
target
ObjectGroup or Sprite to check against the current one
-
[callback]
Function optionalThe function to be called if overlap is positive
Returns:
True if overlapping
Example:
group.collide(otherSprite, explosion);
function explosion(spriteA, spriteB) {
spriteA.remove();
spriteB.score++;
}
contains
-
sprite
Checks if the group contains a sprite.
Parameters:
-
sprite
SpriteThe sprite to search
Returns:
Index or -1 if not found
Creates four edge sprites and adds them to a group. Each edge is just outside of the canvas and has a thickness of 100. After calling this function, the following properties are exposed and populated with sprites: leftEdge, rightEdge, topEdge, bottomEdge
The 'edges' property is populated with a group containing those four sprites.
If this edge sprites have already been created, the function returns the existing edges group immediately.
Returns:
The edges group
displace
-
target
-
callback
Checks if the the group is overlapping another group or sprite. If the overlap is positive the sprites in the group will displace the colliding ones to the closest non-overlapping positions.
The check is performed using the colliders. If colliders are not set they will be created automatically from the image/animation bounding box.
A callback function can be specified to perform additional operations when the overlap occurs. The function will be called for each single sprite overlapping. The parameter of the function are respectively the member of the current group and the other sprite passed as parameter.
Parameters:
-
target
ObjectGroup or Sprite to check against the current one
-
[callback]
Function optionalThe function to be called if overlap is positive
Returns:
True if overlapping
Example:
group.displace(otherSprite, explosion);
function explosion(spriteA, spriteB) {
spriteA.remove();
spriteB.score++;
}
draw
()
Draws all the sprites in the group.
get
-
i
Gets the member at index i.
Parameters:
-
i
NumberThe index of the object to retrieve
indexOf
()
Same as Group.contains
isTouching
-
target
Alias for overlap()
Returns whether or not this group will bounce or collide with another sprite or group. Modifies the each sprite's touching property object.
Parameters:
-
target
ObjectGroup or Sprite to check against the current one
Returns:
True if touching
maxDepth
()
Number
Returns the highest depth in a group
Returns:
The depth of the sprite drawn on the top
minDepth
()
Number
Returns the lowest depth in a group
Returns:
The depth of the sprite drawn on the bottom
overlap
-
target
-
callback
Checks if the the group is overlapping another group or sprite. The check is performed using the colliders. If colliders are not set they will be created automatically from the image/animation bounding box.
A callback function can be specified to perform additional operations when the overlap occurs. The function will be called for each single sprite overlapping. The parameter of the function are respectively the member of the current group and the other sprite passed as parameter.
Parameters:
-
target
ObjectGroup or Sprite to check against the current one
-
[callback]
Function optionalThe function to be called if overlap is positive
Returns:
True if overlapping
Example:
group.overlap(otherSprite, explosion);
function explosion(spriteA, spriteB) {
spriteA.remove();
spriteB.score++;
}
remove
-
item
Removes a sprite from the group. Does not remove the actual sprite, only the affiliation (reference).
Parameters:
-
item
SpriteThe sprite to be removed
Returns:
True if sprite was found and removed
removeSprites
()
Removes all the sprites in the group from the scene.
size
()
Same as group.length
toArray
()
Returns a copy of the group as standard array.