destroy

Destroy the sprite.

sprites.create(null).destroy()

The sprite destroys itself immediately. The sprite is removed from the game and will no longer overlap or collide with any other sprites.

Sprites will also destroy when their lifespan count reaches 0 or they have auto destroy set as a sprite flag when they move off screen.

You can also select an optional particle effect to display at the sprite when it’s destroyed.

Parameters

  • effect: an optional built-in effect to display when the sprite is destroyed.

Example

Gulp the player

Make a Player and an Enemy sprite. The player moves into the open area of the enemy sprite. When an overlap of the two is detected, destroy the Player.

enum SpriteKind {
    Player,
    Enemy
}
let enemy: Sprite = null
let catchBox: Image = null
let player: Sprite = null
let yellowBlock: Image = null

yellowBlock = image.create(16, 16)
yellowBlock.fill(5)
yellowBlock.drawRect(0, 0, 16, 16, 1)
player = sprites.create(yellowBlock, SpriteKind.Player)
player.left = 0
catchBox = image.create(32, 32)
catchBox.drawRect(0, 0, 32, 32, 1)
catchBox.drawLine(0, 6, 0, 25, 0)
enemy = sprites.create(catchBox, SpriteKind.Enemy)
enemy.right = scene.screenWidth() - 16
player.vx = 20
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
    otherSprite.say("Gulp!", 1000)
    sprite.destroy()
})

Show a destroy effect

Create a square sprite. Destroy the sprite after 3 seconds and show a fountain effect.

let square = image.create(32, 32)
square.fill(2)
square.drawRect(0, 0, 32, 32, 1)
let sqSprite = sprites.create(square)
pause(3000)
sqSprite.destroy(effects.fountain)

See also

on destroyed, lifespan, set flag