Show:
Module: p5play

In p5play groups are collections of sprites with similar behavior. 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 properties of standard arrays such as 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() removes the sprite from all the groups it belongs to.

Constructor

Group ()

Defined in lib/p5play.js:1874

Methods

_groupCollide
(
  • type
  • target
  • callback
)
Boolean
private

Defined in lib/p5play.js:2376

Collide each member of group against the target using the given collision type. Return true if any collision occurred. Internal use

Parameters:

  • type String

    one of 'overlap', 'collide', 'displace', 'bounce'

  • target Sprite | Group

    Group or Sprite

  • [callback] Function optional

    on collision.

Returns:

Boolean:

True if any collision/overlap occurred

_rebuildQuadtree ()

Defined in lib/p5play.js:2153

Not for p5play users! This method is used to rebuild quad tree and should only be called on the allSprites group.

add
(
  • s
)

Defined in lib/p5play.js:2060

Adds a sprite to the group. Returns true if the sprite was added because it was not already in the group.

Parameters:

  • s Sprite

    The sprite to be added

addAnimation
(
  • label
  • animation
)

Defined in lib/p5play.js:2299

Adds an animation to the group. The animation should be preloaded in the preload() function using loadAnimation. Animations require a identifying label (string) to change them. Animations are stored in the group but not necessarily displayed until Sprite.changeAnimation(label) is called.

Usage:

  • group.addAnimation(label, animation);

Alternative usages. See SpriteAnimation for more information on file sequences:

  • group.addAnimation(label, firstFrame, lastFrame);
  • group.addAnimation(label, frame1, frame2, frame3...);

Parameters:

  • label String

    SpriteAnimation identifier

  • animation SpriteAnimation

    The preloaded animation

addImage
(
  • label
  • img
)

Defined in lib/p5play.js:2274

Adds an image to the Group. An image will be considered a one-frame animation. The image should be preloaded in the preload() function using p5 loadImage. Animations require a identifying label (string) to change them. The image is stored in the Group but not necessarily displayed until Sprite.changeAnimation(label) is called

Usages:

  • group.addImage(label, image);
  • group.addImage(image);

If only an image is passed no label is specified

Parameters:

  • label String | p5.Image

    Label or image

  • [img] p5.Image optional

    Image

bounce
(
  • target
  • callback
)
Boolean

Defined in lib/p5play.js:1989

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 Sprite | Group

    Group or Sprite to check against the current one

  • [callback] Function optional

    The function to be called if overlap is positive

Returns:

Boolean:

True if overlapping

Example:

group.bounce(otherSprite, explosion);

function explosion(spriteA, spriteB) {
  spriteA.remove();
  spriteB.score++;
}
clear ()

Defined in lib/p5play.js:2168

Removes all references to the group. Does not remove the actual sprites.

collide
(
  • target
  • callback
)
Boolean

Defined in lib/p5play.js:1931

Checks if the the group is overlapping another group or sprite. If the overlap is positive the sprites in the group will be displaced by the colliding one 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 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 Sprite | Group

    Group or Sprite to check against the current one

  • [callback] Function optional

    The function to be called if overlap is positive

Returns:

Boolean:

True if overlapping

Example:

group.collide(otherSprite, explosion);

function explosion(spriteA, spriteB) {
  spriteA.remove();
  spriteB.score++;
}
contains
(
  • sprite
)
Number

Defined in lib/p5play.js:2036

Checks if the group contains a sprite.

Parameters:

  • sprite Sprite

    The sprite to search

Returns:

Number:

Index or -1 if not found

cull
(
  • top|size
  • bottom|cb
  • left
  • right
  • cb
)

Defined in lib/p5play.js:2087

Remove sprites that go outside the culling boundary

Parameters:

  • 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] Function optional

    The function to be called if any sprites in the group are culled

displace
(
  • target
  • callback
)
Boolean

Defined in lib/p5play.js:1960

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 Sprite | Group

    Group or Sprite to check against the current one

  • [callback] Function optional

    The function to be called if overlap is positive

Returns:

Boolean:

True if overlapping

Example:

group.displace(otherSprite, explosion);

function explosion(spriteA, spriteB) {
  spriteA.remove();
  spriteB.score++;
}
draw ()

Defined in lib/p5play.js:2258

Draws all the sprites in the group.

get
(
  • i
)

Defined in lib/p5play.js:2026

Gets the member at index i.

Parameters:

  • i Number

    The index of the object to retrieve

indexOf ()

Defined in lib/p5play.js:2047

Same as Group.contains

maxDepth () Number

Defined in lib/p5play.js:2226

Returns the highest depth in a group

Returns:

Number:

The depth of the sprite drawn on the top

minDepth () Number

Defined in lib/p5play.js:2242

Returns the lowest depth in a group

Returns:

Number:

The depth of the sprite drawn on the bottom

overlap
(
  • target
  • callback
)
Boolean

Defined in lib/p5play.js:1905

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 Sprite | Group

    Group or Sprite to check against the current one

  • [callback] Function optional

    The function to be called if overlap is positive

Returns:

Boolean:

True if overlapping

Example:

group.overlap(otherSprite, explosion);

function explosion(spriteA, spriteB) {
  spriteA.remove();
  spriteB.score++;
}
remove
(
  • item
)
Boolean

Defined in lib/p5play.js:2178

Removes a sprite from the group. Does not remove the actual sprite, only the affiliation (reference).

Parameters:

  • item Sprite

    The sprite to be removed

Returns:

Boolean:

True if sprite was found and removed

removeSprites ()

Defined in lib/p5play.js:2136

Removes all the sprites in the group from the scene.

size ()

Defined in lib/p5play.js:2079

Same as group.length

toArray ()
deprecated

Defined in lib/p5play.js:2211

This method is deprecated, don't use it! This method is included for backwards compatibility only. Deprecated since v2.

It used to return a copy of the group as standard array but now that Group extends Array you can use groups in for loops just like arrays without using this method.

Properties

animations

Object

Defined in lib/p5play.js:1897

Keys are the animation label, values are SpriteAnimation objects.