SpriteAnimation
An SpriteAnimation object contains a series of images (p5.Image) that can be displayed sequentially.
All files must be png images. You must include the directory from the sketch root, and the extension .png
A sprite can have multiple labeled animations, see Sprite.addAnimation and Sprite.changeAnimation, however an animation can be used independently.
An animation can be created either by passing a series of file names, no matter how many or by passing the first and the last file name of a numbered sequence. p5play will try to detect the sequence pattern.
For example if the given filenames are "data/file0001.png" and "data/file0005.png" the images "data/file0003.png" and "data/file0004.png" will be loaded as well.
Table of Contents
Constructor
SpriteAnimation
-
fileName1
-
fileName2
-
fileNameN
Parameters:
-
fileName1
StringFirst file in a sequence OR first image file
-
fileName2
StringLast file in a sequence OR second image file
-
[fileNameN]
String optional multipleAny number of image files after the first two
Example:
var sequenceAnimation;
var glitch;
function preload() {
sequenceAnimation = loadAnimation("data/walking0001.png", "data/walking0005.png");
glitch = loadAnimation("data/dog.png", "data/horse.png", "data/cat.png", "data/snake.png");
}
function setup() {
createCanvas(800, 600);
}
function draw() {
background(0);
animation(sequenceAnimation, 100, 100);
animation(glitch, 200, 100);
}
Methods
changeFrame
-
frame
Changes the current frame.
Parameters:
-
frame
NumberFrame number (starts from 0).
Objects are passed by reference so to have different sprites using the same animation you need to clone it.
Returns:
A clone of the current animation
draw
-
x
-
y
-
r=0
Draws the animation at coordinate x and y. Updates the frames automatically.
Parameters:
-
x
Numberx coordinate
-
y
Numbery coordinate
-
[r=0]
Number optionalrotation
getFrame
()
Number
Returns the current frame number.
Returns:
Current frame (starts from 0)
getFrameImage
()
p5.Image
Returns the current frame image as p5.Image.
Returns:
Current frame image
getHeight
()
Number
Returns the current frame height in pixels. If there is no image loaded, returns 1.
Returns:
Frame height
getImageAt
-
frame
Returns the frame image at the specified frame number.
Parameters:
-
frame
NumberFrame number
Returns:
Frame image
getLastFrame
()
Number
Returns the last frame number.
Returns:
Last frame number (starts from 0)
getWidth
()
Number
Returns the current frame width in pixels. If there is no image loaded, returns 1.
Returns:
Frame width
goToFrame
-
toFrame
Plays the animation forward or backward toward a target frame.
Parameters:
-
toFrame
NumberFrame number destination (starts from 0)
nextFrame
()
Goes to the next frame and stops.
play
()
Plays the animation.
previousFrame
()
Goes to the previous frame and stops.
rewind
()
Rewinds the animation to the first frame.
stop
()
Stops the animation.
Properties
frameChanged
Boolean
True if frame changed during the last draw cycle
frameDelay
Number
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
images
Array
Array of frames (p5.Image)
looping
Boolean
If set to false the animation will stop after reaching the last frame
Default: true
playing
Boolean
True if the animation is currently playing.
Default: true
visible
Boolean
Animation visibility.
Default: true