sprite.setAnimation()

Category:Sprites

Sets the image or animation for the sprite.

Use the Animation tab to load and label images and animations for your sprites.

The default image is a gray rectangle.

Examples

var sprite = createSprite(200, 200);
sprite.setAnimation("car_blue_1");
drawSprites();

Roll a Die

Use 6 different images for a sprite, labeled "dieRed1" through "dieRed6", and randomNumber to roll a die.

// Use 6 different images for a sprite, labeled "dieRed1" through "dieRed6", and randomNumber to roll a die.
var die = createSprite(200,200);
var count=0;
function draw() {
  background("white");
  count=count+1;
  if (count<20) {
    die.setAnimation("dieRed"+randomNumber(1,6));
  }
  drawSprites();
}

Syntax

sprite.setAnimation(label)

Parameters

NameTypeRequired?Description
label String The name of an image or animation loaded in the Animation tab.

Returns

No return value. Changes output in the display after drawSprites() is called.

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 documentation@code.org