Show:
Module: p5.play
Parent Module: p5.play

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.

Constructor

Group ()

Defined in lib/p5.play.js:2290

Methods

add
(
  • sprite
)

Defined in lib/p5.play.js:2345

Adds a sprite to the group.

Parameters:

  • sprite Sprite

    The sprite to be added

bounce
(
  • target
  • callback
)
Boolean

Defined in lib/p5.play.js:2601

Checks if the the group is overlapping another group. 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 member of the group passed as parameter.

Parameters:

  • target Object

    Group to check against the current one

  • [callback] Function optional

    The function to be called if overlap is positive

Returns:

Boolean:

True if overlapping

Example:

sprite.bounce(otherSprite, explosion);

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

clear ()

Defined in lib/p5.play.js:2371

Removes all the sprites in the group from the scene.

clear ()

Defined in lib/p5.play.js:2385

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

contains
(
  • sprite
)
Number

Defined in lib/p5.play.js:2322

Checks if the group contains a sprite.

Parameters:

  • sprite Sprite

    The sprite to search

Returns:

Number:

Index or -1 if not found

displace
(
  • target
  • callback
)
Boolean

Defined in lib/p5.play.js:2567

Checks if the the group is overlapping another group. 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 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 member of the group passed as parameter.

Parameters:

  • target Object

    Group to check against the current one

  • [callback] Function optional

    The function to be called if overlap is positive

Returns:

Boolean:

True if overlapping

Example:

sprite.displace(otherSprite, explosion);

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

draw ()

Defined in lib/p5.play.js:2465

Draws all the sprites in the group.

get
(
  • i
)

Defined in lib/p5.play.js:2312

Gets the member at index i.

Parameters:

  • i Number

    The index of the object to retrieve

maxDepth () Number

Defined in lib/p5.play.js:2423

Returns the highest depth in a group

Returns:

Number:

The depth of the sprite drawn on the top

minDepth () Number

Defined in lib/p5.play.js:2444

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/p5.play.js:2501

Checks if the the group is overlapping another group. 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 member of the group passed as parameter.

Parameters:

  • target Object

    Group to check against the current one

  • [callback] Function optional

    The function to be called if overlap is positive

Returns:

Boolean:

True if overlapping

Example:

sprite.overlap(otherSprite, explosion);

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

overlap
(
  • target
  • callback
)
Boolean

Defined in lib/p5.play.js:2533

Checks if the the group is overlapping another group. 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 member of the group passed as parameter.

Parameters:

  • target Object

    Group to check against the current one

  • [callback] Function optional

    The function to be called if overlap is positive

Returns:

Boolean:

True if overlapping

Example:

sprite.overlap(otherSprite, explosion);

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

remove
(
  • sprite
)
Boolean

Defined in lib/p5.play.js:2395

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

Parameters:

  • sprite Sprite

    The sprite to be removed

Returns:

Boolean:

True if sprite was found and removed