Group
In p5.play 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.
Table of Contents
-
Methods
- add(s)
- addAnimation(labelanimation)
- addImage(labelimg)
- animation(animxy)
- background()
- bounce(targetcallback)
- collide(targetcallback)
- colorPal(cpalette)
- contains(sprite)
- createTiles(tiles)
- cull(top|sizebottom|cbleftrightcb(sprite))
- displace(targetcallback)
- drawSprites(group)
- fill()
- get(i)
- getKeyCode(keyName)
- getSpriteAt(xy)
- keyIsDown(keyName)
- loadAnimation()
- loadSpriteSheet()
- maxDepth()
- minDepth()
- overlap(targetcallback)
- play(sound)
- push(s)
- remove(item)
- removeAll()
- removeSprites()
- size()
- spriteArt(txtscalepalette)
- stroke()
- updateSprites(timeStepvelocityIterationspositionIterations)
- Properties
Constructor
Group
()
Methods
add
-
s
Adds a sprite to the group. Returns true if the sprite was added because it was not already in the group.
Parameters:
-
s
SpriteThe sprite to be added
addAnimation
-
label
-
animation
Adds an animation to the group. This function should be used in the preload p5.js function. You don't need to name the animation if the sprites in the group will only use one animation. See SpriteAnimation for more information.
Uses:
- group.addAnimation(label, animation);
- group.addAnimation(label, firstFrame, lastFrame);
- group.addAnimation(label, frame1, frame2, frame3...);
Parameters:
-
label
StringSpriteAnimation identifier
-
animation
SpriteAnimationThe preloaded animation
addImage
-
label
-
img
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.ImageLabel or image
-
[img]
p5.Image optionalImage
animation
-
anim
-
x
-
y
Displays an animation. Similar to the p5.js image function.
Parameters:
-
anim
SpriteAnimationAnimation to be displayed
-
x
NumberX coordinate
-
y
NumberY coordinate
background
()
Just like the p5.js background function except it also accepts a color pallette code.
bounce
-
target
-
callback
Deprecated, use group.collide instead.
collide
-
target
-
callback
Checks if a sprite in the group is colliding with another sprite or a group. The check is performed using the sprite's physics body (colliders).
A callback function can be specified to perform additional operations when contact occurs. If the target is a group the function will be called for each single sprite colliding. The parameter of the function are respectively the current sprite and the colliding sprite.
Since v3, this function only needs to be called once, it doesn't need to be used in the p5.js draw loop.
Parameters:
Example:
group.collide(otherSprite, explosion);
function explosion(spriteA, spriteB) {
spriteA.remove();
spriteB.score++;
}
colorPal
-
c
-
palette
Gets a color from a color palette
Parameters:
-
c
StringA single character, a key found in the color palette object.
-
palette
Number | ObjectCan be a palette object or number index in the system's palettes array.
Returns:
a hex color string for use by p5.js functions
contains
-
sprite
Checks if the group contains a sprite.
Parameters:
-
sprite
SpriteThe sprite to search
Returns:
Index or -1 if not found
createTiles
-
tiles
Parameters:
-
tiles
String | ArrayString or array of strings
cull
-
top|size
-
bottom|cb
-
left
-
right
-
cb(sprite)
Remove sprites that go outside the culling boundary
Parameters:
-
top|size
NumberThe 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
NumberThe distance that sprites can move below the p5.js canvas before they are removed.
-
[left]
Number optionalThe distance that sprites can move beyond the left side of the p5.js canvas before they are removed.
-
[right]
Number optionalThe distance that sprites can move beyond the right side of the p5.js canvas before they are removed.
-
[cb(sprite)]
Function optionalThe callback is given the sprite that passed the cull boundary, if no callback is given the sprite is removed by default
displace
-
target
-
callback
Deprecated, use group.collide instead.
drawSprites
-
group
This function is called automatically at the end of the p5.js draw loop unless it was already called in the draw loop.
Parameters:
-
group
Groupof sprites, allSprites by default
fill
()
Just like the p5.js fill function except it also accepts a color pallette code.
get
-
i
Gets the member at index i.
Parameters:
-
i
NumberThe index of the object to retrieve
getKeyCode
-
keyName
Get the keyCode of a key
Parameters:
-
keyName
String
Returns:
keyCode
getSpriteAt
-
x
-
y
Returns the sprite at
Parameters:
-
x
Number -
y
Number
Returns:
keyIsDown
-
keyName
Check if key is down.
Parameters:
-
keyName
String
Returns:
true if key is down
Loads an animation. Use this in the preload p5.js function.
Returns:
Loads a Sprite Sheet. Use this in the preload p5.js function.
Returns:
maxDepth
()
Number
Returns the highest depth in a group
Returns:
The depth of the sprite drawn on the top
minDepth
()
Number
Returns the lowest depth in a group
Returns:
The depth of the sprite drawn on the bottom
overlap
-
target
-
callback
Checks if a sprite in the group is overlapping with another sprite or a group. The check is performed using the sprite's physics body (colliders).
A callback function can be specified to perform additional operations when contact occurs. If the target is a group the function will be called for each single sprite overlapping. The parameter of the function are respectively the current sprite and the overlapping sprite.
Since v3, this function only needs to be called once, it doesn't need to be used in the p5.js draw loop.
Parameters:
Example:
group.overlap(otherSprite, pickup);
function pickup(spriteA, spriteB) {
spriteA.remove();
spriteB.itemCount++;
}
play
-
sound
Awaitable function for playing sounds.
Parameters:
-
sound
p5.Sound
Returns:
push
-
s
Adds a sprite to the group. Returns true if the sprite was added because it was not already in the group.
Parameters:
-
s
SpriteThe sprite to be added
remove
-
item
Removes a sprite from the group. Does not remove the actual sprite, only the reference.
Parameters:
-
item
SpriteThe sprite to be removed
Returns:
true if sprite was found and removed
removeAll
()
Removes all the sprites in the group from the scene.
removeSprites
()
Removes all the sprites in the group from the scene.
size
()
Same as group.length
spriteArt
-
txt
-
scale
-
palette
Create pixel art images from a string. Each character in the input string represents a color value defined in the palette object.
Parameters:
-
txt
StringEach character represents a pixel color value
-
scale
NumberThe scale of the image
-
palette
Number | ObjectColor palette
Returns:
A p5.js Image
Example:
let str = ...yyyy .yybyybyy yyyyyyyyyy yybyyyybyy .yybbbbyy ...yyyy
;
let img = spriteArt(str);
stroke
()
Just like the p5.js stroke function except it also accepts a color pallette code.
updateSprites
-
timeStep
-
velocityIterations
-
positionIterations
This function is automatically called at the end of the p5.js draw loop, unless it was already called in the draw loop.
Parameters:
-
timeStep
Number -
velocityIterations
Number -
positionIterations
Number
Properties
allSprites
Unknown
A group of all the sprites.
animations
Object
Keys are the animation label, values are SpriteAnimation objects.
collides
Unknown
Contains all the collision callback functions for this sprite when it comes in contact with other sprites or groups.
gravity
Unknown
Gravity vector
layer
Unknown
The default layer for sprites in the group.
offset.x
Unknown
offset.y
Unknown
overlaps
Unknown
Contains all the overlap callback functions for this sprite when it comes in contact with other sprites or groups.