sprite.y

Category:Sprites

The y position of the center of the sprite.

The default y position is 0. All sprite properties can be both accessed and updated.

Examples

Movement

Move a sprite by incrementing sprite.y within the draw function.

// Move a sprite by incrementing sprite.y within the draw function.
var sprite = createSprite(200, 200);
function draw() {
  background("white");
  sprite.x = sprite.y-5;
  drawSprites();
}

var sprite = createSprite(200,200);
console.log(sprite.y);
drawSprites();
sprite.y = 50;
console.log(sprite.y);
drawSprites();
sprite.y = sprite.y * 7;
console.log(sprite.y);
drawSprites();

Syntax

sprite.y

Returns

The y coordinate of the center of the sprite.

Tips

  • Changing the sprite's location by updating sprite.x and sprite.y does not change the sprite's direction. Instead use the velocity and direction properties/functions: sprite.velocityX, sprite.velocityY, setVelocity(), sprite.setSpeedAndDirection()
  • The display area is 400 pixels x 400 pixels and the upper left corner is (0.0).
  • 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.

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