sprite.scale

Category:Sprites

Shrink or grow a sprite keeping the height to width ratio the same.

For example, a value of 2 will be make the sprite twice as big and a value of 0.5 will make the sprite half as big. Scaling up may make images blurry. The scale should always be a positive number.

The default scale is 1. All sprite properties can be both accessed and updated.

Examples

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

Different Sized Pinwheels

Animate three different sized pinwheels.

// Animate three different sized pinwheels.
var pinwheel1 = createSprite(randomNumber(0, 400), randomNumber(0, 400));
pinwheel1.setAnimation("Pinwheel.png_1");
pinwheel1.scale = 0.1;
var pinwheel2 = createSprite(randomNumber(0, 400), randomNumber(0, 400));
pinwheel2.setAnimation("Pinwheel.png_1");
pinwheel2.scale = 0.2;
var pinwheel3 = createSprite(randomNumber(0, 400), randomNumber(0, 400));
pinwheel3.setAnimation("Pinwheel.png_1");
pinwheel3.scale = 0.05;
function draw() {
  background("white");
  pinwheel1.rotation=pinwheel1.rotation+10;
  pinwheel2.rotation=pinwheel2.rotation-30;
  pinwheel3.rotation=pinwheel3.rotation+50;
  drawSprites();
}

Syntax

sprite.scale

Returns

The scale of the sprite.

Tips

  • Sprites all have the same properties and you use the dot notation (combining the name of the sprite, followed by a dot, with the label of the property) to both access and update the property for that sprite.
  • Any changes to the properties of a sprite will not be seen until after drawSprites() is called.
  • If sprite is scaled, sprite.width and sprite.height are not changed. Use getScaledWidth() and getScaledHeight() instead.
  • If setCollider() is called for a sprite before it is scaled, the collider is not scaled. But if a sprite is first scaled and then the collider set, the scale is also applied to the collider.

Found a bug in the documentation? Let us know at documentation@code.org