API Docs for: 3
Show:

Group Class

Defined in: v3/p5play.js:4352

Look at the Group reference pages before reading these docs.

https://p5play.org/learn/group.html

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 functions and properties of standard arrays such as group.length

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.

Constructor

Group

()

Defined in v3/p5play.js:4352

Methods

add

()

Defined in v3/p5play.js:4590

Alias for group.push

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.

addAni

(
  • name
  • animation
)

Defined in v3/p5play.js:5247

Adds an animation to the sprite. Use this function in the preload p5.js function. You don't need to name the animation if the sprite will only use one animation. See SpriteAnimation for more information.

Parameters:

  • name String

    SpriteAnimation identifier

  • animation SpriteAnimation

    The preloaded animation

Example:

sprite.addAni(name, animation); sprite.addAni(name, frame1, frame2, frame3...); sprite.addAni(name, atlas);

addAnis

()

Defined in v3/p5play.js:5295

Add multiple animations

applyForce

(
  • forceVector
  • [forceOrigin]
)

Defined in v3/p5play.js:4901

Apply a force that is scaled to the sprite's mass.

Parameters:

  • forceVector p5.Vector | Array

    force vector

  • [forceOrigin] p5.Vector | Array optional

    force origin

collided

(
  • target
  • [callback]
)
Boolean

Defined in v3/p5play.js:4783

Returns true on the first frame that the group no longer overlaps with the target sprite or group.

Parameters:

Returns:

Boolean:

collides

(
  • target
  • [callback]
)

Defined in v3/p5play.js:4749

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.

Parameters:

colliding

(
  • target
  • [callback]
)
Number

Defined in v3/p5play.js:4766

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.

Parameters:

Returns:

Number:

frames

contains

()

Defined in v3/p5play.js:4607

Alias for group.includes

cull

(
  • top|size
  • bottom|cb
  • [left]
  • [right]
  • [cb(sprite)]
)
Number

Defined in v3/p5play.js:5079

Remove sprites that go outside the given culling boundary relative to the camera.

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

Number:

The number of sprites culled

draw

()

Defined in v3/p5play.js:5206

Draws all the sprites in the group.

includes

(
  • sprite
)
Number

Defined in v3/p5play.js:4600

Check if a sprite is in the group.

Parameters:

Returns:

Number:

index of the sprite or -1 if not found

move

()

Defined in v3/p5play.js:4914

moveAway

()

Defined in v3/p5play.js:4971

moveTo

()

Defined in v3/p5play.js:4925

moveTowards

()

Defined in v3/p5play.js:4948

orbit

(
  • amount
)

Defined in v3/p5play.js:4994

EXPERIMENTAL! Subject to change in the future!

Rotates the group around its centroid.

Parameters:

  • amount Number

    Amount of rotation

overlapped

(
  • target
  • [callback]
)
Boolean

Defined in v3/p5play.js:4886

Returns true on the first frame that the group no longer overlaps with the target sprite or group.

Parameters:

Returns:

Boolean:

overlapping

(
  • target
  • [callback]
)
Number

Defined in v3/p5play.js:4869

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.

Parameters:

Returns:

Number:

frames

overlaps

(
  • target
  • [callback]
)

Defined in v3/p5play.js:4852

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.

Parameters:

push

(
  • sprites
)
Number

Defined in v3/p5play.js:5031

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.

Parameters:

  • sprites Sprite

    The sprite or sprites to be added

Returns:

Number:

the new length of the group

remove

(
  • item
)
Sprite

Defined in v3/p5play.js:5127

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.

Parameters:

  • item Sprite

    The sprite to be removed

Returns:

Sprite:

the removed sprite

removeAll

()

Defined in v3/p5play.js:5165

Removes all sprites from the group and destroys the group.

update

()

Defined in v3/p5play.js:5229

Updates all the sprites in the group. See sprite.update for more information.

By default, allSprites.update is called after every draw call.

Properties

amount

Number

Defined in v3/p5play.js:4674

Depending on the value that the amount property is set to, the group will either add or remove sprites.

ani

SpriteAnimation

Defined in v3/p5play.js:4615

Reference to the group's current animation.

animation

SpriteAnimation

Defined in v3/p5play.js:4628

Reference to the group's current animation.

animations

SpriteAnimations

Defined in v3/p5play.js:4441

Keys are the animation label, values are SpriteAnimation objects.

anis

SpriteAnimations

Defined in v3/p5play.js:4641

The group's animations.

autoCull

Boolean

Defined in v3/p5play.js:4572

autoCull is a property of the allSprites group only, that controls whether sprites are automatically removed when they are 10,000 pixels away from the camera.

It only needs to be set to false once and then it will remain false for the rest of the sketch, unless changed.

idNum

Number

Defined in v3/p5play.js:4395

Each group has a unique id number. Don't change it! Its useful for debugging.

image

SpriteAnimation

Defined in v3/p5play.js:4662

Reference to the group's current image.

img

SpriteAnimation

Defined in v3/p5play.js:4650

Reference to the group's current image.

length

Unknown

Defined in v3/p5play.js:5057

parent

Number

Defined in v3/p5play.js:4421

The parent group's uid number.

Default: undefined

subgroups

Array

Defined in v3/p5play.js:4411

Groups can have subgroups, which inherit the properties of their parent groups.

Default: []