From fa7588c40071b50e08e67810a9fb3e9e9cc97c77 Mon Sep 17 00:00:00 2001 From: Tom Moran <62551154+Tomhausen@users.noreply.github.com> Date: Thu, 5 Dec 2024 18:57:20 +0000 Subject: [PATCH] added event for when sprites of a certain kind hit the edge of the screen --- SpritesAdvanced.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/SpritesAdvanced.ts b/SpritesAdvanced.ts index c5484e3..b684e72 100644 --- a/SpritesAdvanced.ts +++ b/SpritesAdvanced.ts @@ -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 + } + } + }) + } } \ No newline at end of file