Skip to content

Commit

Permalink
added event for when sprites of a certain kind hit the edge of the sc…
Browse files Browse the repository at this point in the history
…reen
  • Loading branch information
Tomhausen committed Dec 5, 2024
1 parent 74c52b1 commit fa7588c
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions SpritesAdvanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,27 @@ namespace spriteutils {
// return condition
// })
// }

/**
* The attached code will run when a sprite of that kind collides with the edge of the screen
* @param kind the sprite kind we check the collision on
* @param handler the code to run on the collision
*/
//% group="Sprite"
//% weight=98
//% blockId=onSpriteOfKindHitsEdgeOfScreen
//% draggableParameters="reporter"
//% block="on sprite of kind $kind=spritekind hits edge of screen"
export function onSpriteOfKindHitsEdgeOfScreen(kind: number, handler: (sprite: Sprite) => void) {
game.onUpdate(() => {
for (let sprite of sprites.allOfKind(kind)) {
if (sprite.left <= scene.cameraProperty(CameraProperty.Left) ||
sprite.right <= scene.cameraProperty(CameraProperty.Right) ||
sprite.top <= scene.cameraProperty(CameraProperty.Top) ||
sprite.bottom <= scene.cameraProperty(CameraProperty.Bottom)) {
handler
}
}
})
}
}

0 comments on commit fa7588c

Please sign in to comment.