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

A Sprite is the main building block of p5.game: an element able to store images or animations with a set of properties such as position and visibility. A Sprite can have a collider that defines the active area to detect collisions or overlappings with other sprites and mouse interactions.

To create a Sprite, use createSprite()

Methods

addAnimation
(
  • label
  • animation
)

Defined in lib/p5.game.js:2234

Adds an animation to the sprite. 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 sprite but not necessarily displayed until Sprite.changeAnimation(label) is called.

Usage:

  • sprite.addAnimation(label, animation);

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

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

Parameters:

  • label String

    Animation identifier

  • animation Animation

    The preloaded animation

addImage
(
  • label
  • img
)

Defined in lib/p5.game.js:2206

Adds an image to the sprite. 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 sprite but not necessarily displayed until Sprite.changeAnimation(label) is called

Usages:

  • sprite.addImage(label, image);
  • sprite.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

addToGroup
(
  • group
)

Defined in lib/p5.game.js:2080

Adds the sprite to an existing group

Parameters:

  • group Object
addVelocity
(
  • speed
  • angle
)

Defined in lib/p5.game.js:2151

Pushes the sprite in a direction defined by an angle. The force is added to the current velocity.

Parameters:

  • speed Number

    Scalar speed to add

  • angle Number

    Direction in degrees

bounce
(
  • target
  • callback
)
Boolean

Defined in lib/p5.game.js:2519

Checks if the the sprite is overlapping another sprite or a 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 collision occours. 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.

Parameters:

  • target Object

    Sprite or 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++;
}
changeAnimation
(
  • label
)

Defined in lib/p5.game.js:2318

Changes the displayed animation. See Animation for more control over the sequence.

Parameters:

  • label String

    Animation identifier

changeImage
(
  • label
)

Defined in lib/p5.game.js:2297

Changes the displayed image/animation. Equivalent to changeAnimation

Parameters:

  • label String

    Image/Animation identifier

collide
(
  • target
  • callback
)
Boolean

Defined in lib/p5.game.js:2456

Checks if the the sprite is overlapping another sprite or a group. If the overlap is positive the current sprite will be displaced by the colliding one in the closest non-overlapping position.

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 collision occours. 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.

Parameters:

  • target Object

    Sprite or 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.collide(otherSprite, explosion);

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

Defined in lib/p5.game.js:2488

Checks if the the sprite is overlapping another sprite or a group. If the overlap is positive the current sprite will displace the colliding one to the closest non-overlapping position.

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 collision occours. 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.

Parameters:

  • target Object

    Sprite or 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++;
}
display ()
private final

Defined in lib/p5.game.js:1926

Manages the positioning, scale and rotation of the sprite Called automatically, it should not be overridden

draw ()

Defined in lib/p5.game.js:1989

Manages the visuals of the sprite. It can be overridden with a custom drawing function. The 0,0 point will be the center of the sprite. Example: sprite.draw = function() { ellipse(0,0,10,10) } Will display the sprite as circle.

getAnimationLabel () String

Defined in lib/p5.game.js:2308

Returns the label of the current animation

Returns:

String:

label Image/Animation identifier

getBoundingBox ()

Defined in lib/p5.game.js:1852

Returns a the bounding box of the current image

getDirection () Number

Defined in lib/p5.game.js:2056

Calculates the movement's direction in degrees.

Returns:

Number:

Angle in degrees

getDirectionTo
(
  • pointX
  • pointY
)
Number

Defined in lib/p5.game.js:2170

Get the angle between this sprite and another point. This can be used to rotate or move a sprite toward that point.

Parameters:

  • pointX Number

    point x coordinite

  • pointY Number

    point y coordinate

Returns:

Number:

direction in degrees

Example:

# Moves the sprite toward the mouse cursor
let speed = 10;
let direction = mySprite.getDirectionTo(mouseX, mouseY);
mySprite.setVelocity(speed, direction);
getDistanceTo
(
  • pointX
  • pointY
)
Number

Defined in lib/p5.game.js:2190

Get the distance between this sprite and another point.

Parameters:

  • pointX Number

    point x coordinite

  • pointY Number

    point y coordinate

Returns:

Number:

distance in pixels

Example:

var distanceToMouse = mySprite.getDistanceTo(mouseX, mouseY);
getSpeed () Number

Defined in lib/p5.game.js:2046

Calculates the scalar speed.

Returns:

Number:

Scalar speed

isTouching
(
  • target
)
Boolean

Defined in lib/p5.game.js:2439

Checks if the sprite is overlapping another sprite or group. This is an alias for the overlap() function, but without a callback.

Parameters:

  • target Object

    Sprite or group to check against the current one

Returns:

Boolean:

True if overlapping

Example:

if (player.isTouching(coin)) {
    score++;
}
limitSpeed
(
  • max
)

Defined in lib/p5.game.js:2093

Limits the scalar speed.

Parameters:

  • max Number

    Max speed: positive number

mirrorX
(
  • dir
)
Number

Defined in lib/p5.game.js:1872

Sets the sprite's horizontal mirroring. If 1 the images displayed normally If -1 the images are flipped horizontally If no argument returns the current x mirroring

Parameters:

  • dir Number

    Either 1 or -1

Returns:

Number:

Current mirroring if no parameter is specified

mirrorY
(
  • dir
)
Number

Defined in lib/p5.game.js:1889

Sets the sprite's vertical mirroring. If 1 the images displayed normally If -1 the images are flipped vertically If no argument returns the current y mirroring

Parameters:

  • dir Number

    Either 1 or -1

Returns:

Number:

Current mirroring if no parameter is specified

mouseUpdate ()

Defined in lib/p5.game.js:1718

Updates the sprite mouse states and triggers the mouse events: onMouseOver, onMouseOut, onMousePressed, onMouseReleased

overlap
(
  • target
  • callback
)
Boolean

Defined in lib/p5.game.js:2410

Checks if the the sprite is overlapping another sprite or a 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. 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 colliding sprite.

Parameters:

  • target Object

    Sprite or 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++;
}
overlapPixel
(
  • pointX
  • pointY
)
Boolean

Defined in lib/p5.game.js:2342

Checks if the given point corresponds to a transparent pixel in the sprite's current image. It can be used to check a point collision against only the visible part of the sprite.

Parameters:

  • pointX Number

    x coordinate of the point to check

  • pointY Number

    y coordinate of the point to check

Returns:

Boolean:

result True if non-transparent

overlapPoint
(
  • pointX
  • pointY
)
Boolean

Defined in lib/p5.game.js:2377

Checks if the given point is inside the sprite's collider.

Parameters:

  • pointX Number

    x coordinate of the point to check

  • pointY Number

    y coordinate of the point to check

Returns:

Boolean:

result True if inside

remove ()

Defined in lib/p5.game.js:2014

Removes the Sprite from the sketch. The removed Sprite won't be drawn or updated anymore.

setCollider
(
  • type
  • offsetX
  • offsetY
  • width
  • height
)

Defined in lib/p5.game.js:1791

Sets a collider for the sprite.

In p5.game a Collider is an invisible circle or rectangle that can have any size or position relative to the sprite and which will be used to detect collisions and overlapping with other sprites, or the mouse cursor.

If the sprite is checked for collision, bounce, overlapping or mouse events a collider is automatically created from the width and height parameter passed at the creation of the sprite or the from the image dimension in case of animated sprites.

Often the image bounding box is not appropriate as the active area for collision detection so you can set a circular or rectangular sprite with different dimensions and offset from the sprite's center.

There are four ways to call this method:

  1. setCollider("rectangle")
  2. setCollider("rectangle", offsetX, offsetY, width, height)
  3. setCollider("circle")
  4. setCollider("circle", offsetX, offsetY, radius)

Parameters:

  • type String

    Either "rectangle" or "circle"

  • offsetX Number

    Collider x position from the center of the sprite

  • offsetY Number

    Collider y position from the center of the sprite

  • width Number

    Collider width or radius

  • height Number

    Collider height

setDefaultCollider ()

Defined in lib/p5.game.js:1684

Creates a default collider matching the size of the placeholder rectangle or the bounding box of the image.

setDirection
(
  • direction
)

Defined in lib/p5.game.js:2126

Set the direction of a moving sprite.

Note that direction cannot be set if speed is 0. Use setSpeed(speed) before setting direction, or use setVelocity(speed, direction) to set both at once.

Parameters:

  • direction Number

    Direction in degrees

setSpeed
(
  • speed
)

Defined in lib/p5.game.js:2113

Set the speed of the sprite in pixels per frame. The sprite's position will automatically update on each frame.

Parameters:

  • speed Number

    Scalar speed

setVelocity
(
  • speed
  • direction
)

Defined in lib/p5.game.js:2031

Sets the velocity vector given a speed and direction (in degrees). To set velocity x,y components directly, use velocity.x or velocity.y instead.

Parameters:

  • speed Number
  • direction Number

    (in degrees)

update ()

Defined in lib/p5.game.js:1549

Updates the sprite. Called automatically at the beginning of the draw cycle.

Properties

_drawnWithCamera

Boolean private

Defined in lib/p5.game.js:1509

Internal variable to keep track of whether this sprite is drawn while the camera is active. Used in Sprite.update() to know whether to use camera mouse coordinates.

Default: false

_rotation

Number private

Defined in lib/p5.game.js:1223

Internal rotation variable (expressed in degrees). Note: external callers access this through the rotation property above.

Default: 0

animation

Animation

Defined in lib/p5.game.js:1501

Reference to the current animation.

bottom

Number

Defined in lib/p5.game.js:1048

The sprite's bottom edge on the Y-axis

centerX

Number

Defined in lib/p5.game.js:988

The sprite's center on the X-axis (alias for Sprite.position.x)

centerY

Number

Defined in lib/p5.game.js:1003

The sprite's center on the Y-axis (alias for Sprite.position.y)

collider

Object

Defined in lib/p5.game.js:1127

The sprite's current collider. It can either be an Axis Aligned Bounding Box (a non-rotated rectangle) or a circular collider. If the sprite is checked for collision, bounce, overlapping or mouse events the collider is automatically created from the width and height of the sprite or from the image dimension in case of animate sprites

You can set a custom collider with Sprite.setCollider

debug

Boolean

Defined in lib/p5.game.js:1470

If set to true, draws an outline of the collider, the depth, and center.

Default: false

depth

Number

Defined in lib/p5.game.js:1256

Determines the rendering order within a group: a sprite with lower depth will appear below the ones with higher depth.

Note: drawing a group before another with drawSprites will make its members appear below the second one, like in normal p5 canvas drawing.

Default: One more than the greatest existing sprite depth, when calling createSprite(). When calling new Sprite() directly, depth will initialize to 0 (not recommended).

friction

Number

Defined in lib/p5.game.js:1115

Friction factor, reduces the sprite's velocity. The friction should be close to 0 (eg. 0.01) 0: no friction 1: full friction

Default: 0

groups

Array

Defined in lib/p5.game.js:1488

Groups the sprite belongs to, including allSprites

height

Number

Defined in lib/p5.game.js:1403

Height of the sprite's current image. If no images or animations are set it's the height of the placeholder rectangle.

Default: 100

ignoreCollisions

Boolean

Defined in lib/p5.game.js:1295

Whether or not collisions are ignored. Set to true to turn collision handling off for this sprite.

Default: false

immovable

Boolean

Defined in lib/p5.game.js:1176

If set to true the sprite won't bounce or be displaced by collisions Simulates an infinite mass or an anchored object.

Default: false

left

Number

Defined in lib/p5.game.js:1063

The sprite's left edge on the X-axis

life

Number

Defined in lib/p5.game.js:1458

Cycles before self removal. Set it to initiate a countdown, every draw cycle the property is reduced by 1 unit. At 0 it will call a sprite.remove() Disabled if set to -1.

Default: -1

mass

Number

Defined in lib/p5.game.js:1165

The mass determines the velocity transfer when sprites bounce against each other. See Sprite.bounce The higher the mass the least the sprite will be affected by collisions.

Default: 1

maxSpeed

Number

Defined in lib/p5.game.js:1105

Set a limit to the sprite's scalar speed regardless of the direction. The value can only be positive. If set to -1, there's no limit.

Default: -1

mouseActive

Boolean

Defined in lib/p5.game.js:1306

If set to true sprite will track its mouse state. the properties mouseIsPressed and mouseIsOver will be updated. Note: automatically set to true if the functions onMouseReleased or onMousePressed are set.

Default: false

mouseIsOver

Boolean

Defined in lib/p5.game.js:1318

True if mouse is on the sprite's collider. Read only.

mouseIsPressed

Boolean

Defined in lib/p5.game.js:1327

True if mouse is pressed on the sprite's collider. Read only.

originalHeight

Number

Defined in lib/p5.game.js:1439

Unscaled height of the sprite If no images or animations are set it's the height of the placeholder rectangle.

Default: 100

originalWidth

Number

Defined in lib/p5.game.js:1428

Unscaled width of the sprite If no images or animations are set it's the width of the placeholder rectangle.

Default: 100

position

p5.Vector

Defined in lib/p5.game.js:951

The sprite's position of the sprite as a vector (x,y).

previousPosition

p5.Vector

Defined in lib/p5.game.js:1078

The sprite's position at the beginning of the last update as a vector (x,y).

removed

Boolean

Defined in lib/p5.game.js:1450

True if the sprite has been removed.

restitution

Number

Defined in lib/p5.game.js:1189

Coefficient of restitution. The velocity lost after bouncing. 1: perfectly elastic, no energy is lost 0: perfectly inelastic, no bouncing less than 1: inelastic, this is the most common in nature greater than 1: hyper elastic, energy is increased like in a pinball bumper

Default: 1

right

Number

Defined in lib/p5.game.js:1033

The sprite's right edge on the X-axis

rotateToDirection

Boolean

Defined in lib/p5.game.js:1245

Automatically lock the rotation property of the visual element (image or animation) to the sprite's movement direction and vice versa.

Default: false

rotation

Number

Defined in lib/p5.game.js:1202

Rotation in degrees of the visual element (image or animation) Note: this is not the movement's direction, see getDirection.

Default: 0

rotationSpeed

Number

Defined in lib/p5.game.js:1234

Rotation change in degrees per frame of thevisual element (image or animation) Note: this is not the movement's direction, see getDirection.

Default: 0

scale

Number

Defined in lib/p5.game.js:1272

Determines the sprite's scale. Example: 2 will be twice the native size of the visuals, 0.5 will be half. Scaling up may make images blurry.

Default: 1

shapeColor

Color

Defined in lib/p5.game.js:1479

If no image or animations are set this is color of the placeholder rectangle

top

Number

Defined in lib/p5.game.js:958

The sprite's position on the X-axis (alias for Sprite.position.x)

top

Number

Defined in lib/p5.game.js:973

The sprite's position on the Y-axis (alias for Sprite.position.y)

top

Number

Defined in lib/p5.game.js:1018

The sprite's top edge on the Y-axis

touching

Object

Defined in lib/p5.game.js:1148

Object containing information about the most recent collision/overlapping To be typically used in combination with Sprite.overlap or Sprite.collide functions. The properties are touching.left, touching.right, touching.top, touching.bottom and are either true or false depending on the side of the collider.

velocity

p5.Vector

Defined in lib/p5.game.js:1096

The sprite's velocity as a vector (x,y) Velocity is speed broken down to its vertical and horizontal components.

visible

Boolean

Defined in lib/p5.game.js:1286

The sprite's visibility.

Default: true

width

Number

Defined in lib/p5.game.js:1378

Width of the sprite's current image. If no images or animations are set it's the width of the placeholder rectangle.

Default: 100