sprite.destroy()
Category:Sprites
Removes the sprite from the animation.
The removed sprite won't be drawn or updated anymore. Use createSprite()
to re-create a sprite.
Examples
// Mouse over a randomly moving sprite to destroy it.
var sprite = createSprite(200, 200, 50, 50);
function draw() {
if (mouseIsOver(sprite)) {
sprite.destroy();
}
background("white");
drawSprites();
sprite.x = randomNumber(0, 400);
sprite.y = randomNumber(0, 400);
}
Rain, Rain Go Away
The umbrella removes the raindrops.
// The umbrella removes the raindrops.
var umbrella = createSprite(200, 200);
umbrella.setAnimation("umbrella");
umbrella.setCollider("circle", 0, 10);
var raindrop = createSprite(200, 200);
raindrop.setAnimation("raindrop");
raindrop.scale = 0.1;
raindrop.setCollider("circle");
raindrop.x = randomNumber(100, 300);
raindrop.y = 0;
raindrop.velocityY = 10;
function draw() {
background("white");
if (umbrella.displace(raindrop)) {
raindrop.destroy();
raindrop = createSprite(200, 200);
raindrop.setAnimation("raindrop");
raindrop.scale = 0.1;
raindrop.setCollider("circle");
raindrop.x = randomNumber(100, 300);
raindrop.y = 0;
raindrop.velocityY = 10;
}
drawSprites();
if (raindrop.y > 400) {
raindrop.x = randomNumber(100, 300);
raindrop.y = 0;
}
}
Syntax
sprite.destroy()
Returns
No return value. Changes output in the display after drawSprites() is called
Found a bug in the documentation? Let us know at support@code.org.