distance between
Problem it solves
You need to know how far away an enemy, power-up, or another sprite is from the player before doing something—playing a sound, firing a projectile, or ending the game.
What this block does
Returns the distance in pixels between the centers of two sprites, points, or tile locations. If either sprite is missing or destroyed, it returns 0.
The block
Block ID: spriteutilextdistbw
Parameters
| Name | Type | Default | Meaning |
|---|---|---|---|
| a | Sprite / Location / Point | mySprite | First point to measure from |
| b | Sprite / Location / Point | myEnemy | Second point to measure to |
Returns
number — The distance in pixels.
Example
let mySprite = sprites.create(img`.`, SpriteKind.Player) let myEnemy = sprites.create(img`.`, SpriteKind.Enemy) let distance = spriteutils.distanceBetween(mySprite, myEnemy) console.log(distance)
Add the extension
Add github:jwunderl/arcade-sprite-util to your project, or open: Import arcade-sprite-util.