Group

Group

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.
Source:

Members

amount :Number

Depending on the value that the amount property is set to, the group will either add or remove sprites.
Source:
Type:
  • Number

ani :SpriteAnimation

Reference to the group's current animation.
Source:
Type:

animation :SpriteAnimation

Reference to the group's current animation.
Source:
Type:

anis :SpriteAnimations

The group's animations.
Source:
Type:
  • SpriteAnimations

image :p5.Image

Reference to the group's current image.
Source:
Type:
  • p5.Image

img :p5.Image

Reference to the group's current image.
Source:
Type:
  • p5.Image

Methods

applyForce(forceVector, forceOriginopt)

Apply a force that is scaled to the sprite's mass.
Source:
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.
Source:
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.
Source:
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.
Source:
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.
Source:
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

draw()

Draws all the sprites in the group.
Source:

get(i)

Gets the member at index i.
Deprecated:
  • get
Source:
Parameters:
Name Type Description
i Number The index of the object to retrieve

move()

Source:

moveAway()

Source:

moveTo()

Source:

moveTowards()

Source:

orbit(amount)

EXPERIMENTAL! Subject to change in the future! Rotates the group around its centroid.
Source:
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.
Source:
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.
Source:
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.
Source:
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.
Source:
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.
Source:
Parameters:
Name Type Description
item Sprite The sprite to be removed
Returns:
Type:
Sprite
the removed sprite

removeAll()

Removes all sprites from the group and destroys the group.
Source:

size()

Alias for group.length
Deprecated:
  • Yes
Source:

toString() → {String}

Returns the group's unique identifier.
Source:
Returns:
Type:
String
groupID

update()

Updates all the sprites in the group. See sprite.update for more information. By default, allSprites.update is called after every draw call.
Source: