API Docs for: 3
Show:

SpriteAnimation Class

Defined in: v3/p5play.js:3474

Look at the Animation reference pages before reading these docs. The SpriteAnimation constructor can be used in multiple ways.

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

A SpriteAnimation object contains a series of images (p5.Image objects) that can be displayed sequentially.

A sprite can have multiple labeled animations, see Sprite.addAnimation and Sprite.changeAnimation, but you can also create animations that can be used without being added to a sprite first.

An animation can be created either from a list of images or sequentially numbered images. p5play will try to detect the sequence pattern.

For example if the image file path is "image1.png" and the last frame index is 3 then "image2.png" and "image3.png" will be loaded as well.

Constructor

SpriteAnimation

(
  • images
)

Defined in v3/p5play.js:3474

Parameters:

  • images ...p5.Image multiple
    • p5.Image objects to be used as frames

Example:

let shapeShifter = new SpriteAnimation("dog.png", "cat.png", "snake.png");

Methods

clone

() SpriteAnimation

Defined in v3/p5play.js:3895

Make a copy of the animation.

Returns:

SpriteAnimation:

A copy of the animation.

draw

(
  • x
  • y
  • [r]
  • [sx]
  • [sy]
)

Defined in v3/p5play.js:3916

Draws the animation at coordinate x and y. Updates the frames automatically.

Optional parameters effect the current draw cycle only and are not saved between draw cycles.

Parameters:

  • x Number

    horizontal position

  • y Number

    vertical position

  • [r] Number optional

    rotation

  • [sx] Number optional

    scale x

  • [sy] Number optional

    scale y

frameImage

() p5.Image

Defined in v3/p5play.js:4199

Returns the current frame as p5.Image.

Returns:

p5.Image:

Current frame image

goToFrame

(
  • toFrame
)

Defined in v3/p5play.js:4135

Plays the animation forward or backward toward a target frame.

Parameters:

  • toFrame Number

    Frame number destination (starts from 0)

Returns:

[Promise] a promise that resolves when the animation completes

loop

()

Defined in v3/p5play.js:4073

Plays the animation forwards and loops it.

nextFrame

()

Defined in v3/p5play.js:4109

Goes to the next frame and stops.

noLoop

()

Defined in v3/p5play.js:4083

Prevents the animation from looping

pause

()

Defined in v3/p5play.js:4040

Pauses the animation.

play

()

Defined in v3/p5play.js:4022

Plays the animation, starting from the specified frame.

Returns:

[Promise] a promise that resolves when the animation completes

previousFrame

()

Defined in v3/p5play.js:4122

Goes to the previous frame and stops.

rewind

()

Defined in v3/p5play.js:4060

Plays the animation backwards. Equivalent to ani.goToFrame(0)

Returns:

[Promise] a promise that resolves when the animation completes rewinding

stop

()

Defined in v3/p5play.js:4050

Stops the animation. Alt for pause.

Properties

endOnFirstFrame

Boolean

Defined in v3/p5play.js:3582

Ends the loop on frame 0 instead of the last frame. This is useful for animations that are symmetric. For example a walking cycle where the first frame is the same as the last frame.

Default: false

frame

Number

Defined in v3/p5play.js:3526

The index of the current frame that the animation is on.

frameChanged

Boolean

Defined in v3/p5play.js:3594

True if frame changed during the last draw cycle

frameDelay

Number

Defined in v3/p5play.js:3846

Delay between frames in number of draw cycles. If set to 4 the framerate of the animation would be the sketch framerate divided by 4 (60fps = 15fps)

Default: 4

frames

Array

Defined in v3/p5play.js:4265

The frames of the animation.

h

Number

Defined in v3/p5play.js:4241

Height of the animation.

height

Number

Defined in v3/p5play.js:4250

Height of the animation.

images

Array

Defined in v3/p5play.js:4279

The frames of the animation. Alt for ani.frames

lastFrame

Number

Defined in v3/p5play.js:4188

Returns the index of the last frame.

looping

Boolean

Defined in v3/p5play.js:3572

If set to false the animation will stop after reaching the last frame

Default: true

name

String

Defined in v3/p5play.js:3505

The name of the animation

offset

Object x and y keys

Defined in v3/p5play.js:3538

The offset is how far the animation should be placed from the location it is played at.

Example:

offset.x = 16;

playing

Boolean

Defined in v3/p5play.js:3554

True if the animation is currently playing.

Default: true

scale

Number | Object

Defined in v3/p5play.js:3873

The animation's scale.

Can be set to a number to scale both x and y or an object with x and/or y properties.

Default: 1

visible

Boolean

Defined in v3/p5play.js:3563

Animation visibility.

Default: true

w

Number

Defined in v3/p5play.js:4217

Width of the animation.

width

Number

Defined in v3/p5play.js:4226

Width of the animation.