sprite.x

Category:Sprites

The x position of the center of the sprite.

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

Examples

var sprite = createSprite(200, 200);
console.log(sprite.x);
drawSprites();
sprite.x = 50;
console.log(sprite.x);
drawSprites();
sprite.x = sprite.x+300;
console.log(sprite.x);
drawSprites();

Movement

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

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

Syntax

sprite.x

Returns

The x 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