Group

Group

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

Extends

Classes

Group

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 :Ani

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

animation :Ani

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

anis :Anis

The group's animations.
Source:
Type:

diameter :Number

Source:
Type:
  • Number

height :Number

Source:
Type:
  • Number

image :p5.Image

The group's image.
Source:
Type:
  • p5.Image

img :p5.Image

Alias for `group.image`.
Source:
Type:
  • p5.Image

velocity :p5.Vector

The sprite's velocity vector {x, y}
Default Value:
  • {x: 0, y: 0}
Source:
Type:
  • p5.Vector

width :Number

Source:
Type:
  • Number

Methods

applyForce()

Source:

applyForceScaled()

Source:

applyTorque()

Source:

attractTo()

Source:

collided(target, callbackopt) → {Boolean}

Returns true on the first frame that the group no longer overlaps with the target group.
Source:
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.
Source:
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.
Source:
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.
Source:
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);

draw()

Draws all the sprites in the group.
Source:

move()

Source:

moveAway()

Source:

moveTo()

Source:

moveTowards()

Source:

overlapped(target, callbackopt) → {Boolean}

Returns true on the first frame that the group no longer overlaps with the target group.
Source:
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.
Source:
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.
Source:
Parameters:
Name Type Attributes Description
target Group
callback function <optional>

pop() → {Sprite}

Removes the last sprite in the group.
Source:
Returns:
Type:
Sprite
the removed sprite or undefined if the group is empty

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 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.
Source:
Parameters:
Name Type Description
item Sprite | Number the sprite to be removed or its index
Returns:
Type:
Sprite
the removed sprite or undefined if the specified sprite was not found

removeAll()

Removes all the sprites in the group from the world and every other group they belong to. Does not delete the group itself.
Source:

shift() → {Sprite}

Removes the last sprite in the group.
Source:
Returns:
Type:
Sprite
the removed sprite or undefined if the group is empty

size()

Alias for group.length
Deprecated:
  • Yes
Source:

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.
Source:
Parameters:
Name Type Description
idx Number index
amount Number number of sprites to remove
Returns:
Type:
Array:.<Sprite:>
the removed sprites

toString() → {String}

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

unshift() → {Number}

Not supported!
Source:
Returns:
Type:
Number
the new length of the group

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: