sprite.setFrame()
Use the frame number to set the frame for the animation for a sprite.
Use the Animation tab to load multiple images which make up the frames for a labeled animation for your sprite. By default all frames are displayed one after another in round robin fashion. The speed is set by the slider in the Animation tab. Use sprite.play()
to restart the animation. Use sprite.pause()
to stop the animation.
Examples
Happy, Surprised, Sad
Use the mouse to move the smiley face. High in the display for happy, middle of the display for surprised, low in the display for sad.
// Use the mouse to move the smiley face.
// High in the display for happy, middle of the display for surprised, low in the display for sad.
var sprite = createSprite(200, 200);
sprite.setAnimation("smiley");
sprite.pause();
function draw() {
background("white");
drawSprites();
sprite.x = World.mouseX;
sprite.y = World.mouseY;
if (sprite.y < 100) {
sprite.setFrame(0);
} else if (sprite.y > 300) {
sprite.setFrame(1);
} else {
sprite.setFrame(2);
}
}
Syntax
sprite.setFrame(frame)
Parameters
Name | Type | Required? | Description |
---|---|---|---|
frame | Number | The frame number of the animation you want the sprite to use. The first frame is frame 0. |
Returns
Tips
- Sprites all have the same functions and you use the dot notation (combining the name of the sprite, followed by a dot, with the function name) to call the function for that sprite.
- Any changes to the properties of a sprite will not be seen until after
drawSprites()
is called.
Found a bug in the documentation? Let us know at support@code.org.