Creates a new sprite and assigns it to the variable specified.
Sprites are used to make complex and interesting animations and games. A sprite is able to store images or animations with a set of properties such as position and visibility. Sprites also have properties to report interactions with other sprites or the mouse.
var sprite = createSprite(); drawSprites();
A later sprite overwrites and earlier sprite.
// A later sprite overwrites an earlier sprite. var sprite1 = createSprite(200,200,100,100); var sprite2 = createSprite(200,200,50,100); sprite2.shapeColor="red"; drawSprites();
Using 26 different images for stickers, use randomNumber to pick twenty different stickers randomly, and use randomNumber again to pick random places for each sticker.
// Use 10 different images for stickers, and randomNumber to place the stickers var count = 0; function draw() { background("white"); count = count + 1; if (count < 20) { var sprite = createSprite(200, 200); sprite.setAnimation("sticker" + randomNumber(1,10)); sprite.x = randomNumber(40, 360); sprite.y = randomNumber(40, 360); } drawSprites(); }
var sprite = createSprite(x, y, width, height)
Name | Type | Required? | Description |
---|---|---|---|
x | Number | The x-location in pixels of the center of the sprite from left to right on the display. Default x value is 0. Should be a number from 0 to 400, but negative numbers will center the sprite to the left of the display and numbers greater than 400 will center the sprite to the right of the display (possibly unseen). | |
y | Number | The y-location in pixels of the center of the sprite from top to bottom on the display. Default y value is 0. Should be a number from 0 to 400, but negative numbers will center the sprite above the display and numbers greater than 400 will start the sprite below the display (possibly unseen). | |
width | Number | The horizontal width in pixels of the default sprite shape, a rectangle. Default width is 100. Should be a positive number. | |
height | Number | The vertical height in pixels of the default sprite shape, a rectangle. Default height is 100. Should be a positive number. |
Returns the sprite and assigns it to the variable specified.
depth
property is changed.setAnimation
command. All images must be first loaded and given a label using the Animation tab above the display window in Game Lab.Found a bug in the documentation? Let us know at documentation@code.org