Show:
Module: p5.game
Parent Module: p5.game

In p5.game 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.game.js:2945

Methods

_groupCollide
(
  • type
  • target
  • callback
)
Boolean
private

Defined in lib/p5.game.js:3195

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 Object

    Group or Sprite

  • [callback] Function optional

    on collision.

Returns:

Boolean:

True if any collision/overlap occurred

add
(
  • s
)

Defined in lib/p5.game.js:3026

Adds a sprite to the group.

Parameters:

  • s Sprite

    The sprite to be added

bounce
(
  • target
  • callback
)
Boolean

Defined in lib/p5.game.js:3299

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 Object

    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/p5.game.js:3068

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

collide
(
  • target
  • callback
)
Boolean

Defined in lib/p5.game.js:3241

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 Object

    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/p5.game.js:3002

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.game.js:3270

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 Object

    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/p5.game.js:3160

Draws all the sprites in the group.

get
(
  • i
)

Defined in lib/p5.game.js:2992

Gets the member at index i.

Parameters:

  • i Number

    The index of the object to retrieve

indexOf ()

Defined in lib/p5.game.js:3013

Same as Group.contains

maxDepth () Number

Defined in lib/p5.game.js:3118

Returns the highest depth in a group

Returns:

Number:

The depth of the sprite drawn on the top

minDepth () Number

Defined in lib/p5.game.js:3134

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.game.js:3214

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 Object

    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/p5.game.js:3078

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

removeAll ()

Defined in lib/p5.game.js:3051

Removes all the sprites in the group from the scene.

size ()

Defined in lib/p5.game.js:3043

Same as group.length

toArray ()

Defined in lib/p5.game.js:3110

Returns a copy of the group as standard array.

Properties

debug

Boolean

Defined in lib/p5.game.js:3150

Sets each sprite in this group to debug mode. This draws an outline of the collider, the depth, and center.

Default: false

ignoreCollisions

Boolean

Defined in lib/p5.game.js:2979

Whether or not collisions are ignored in this group. Can be used to toggle collisions off for the whole group. If set to false, no sprites will trigger a collision, regardless of the individual sprite's ignoreCollisions property. If set to true, all sprites may trigger a collision, except those whose individual ignoreCollisions setting is true.

Default: false

visible

Boolean

Defined in lib/p5.game.js:2968

The group's visibility. If set to false, all sprites will be invisible, regardless of the individual sprite's visible property. If set to true, all sprites will be visible, except those whose visible property is false.

Default: true