There are two possibilities here… or 3 depending on how deep in the code you are willing to dive…
If you are trying to run a bit of code when a certain type of enemy dies, there is a sprite event for that:
sprites.onDestroyed(SpriteKind.Enemy, function (sprite: Sprite) {
})
That function will run whenever a sprite of kind “Enemy” dies, but you can change the sprite type of course. That “sprite” variable holds the sprite that just died.
There is also an event for individual sprites, and I think that would just be like
mySprite.onDestroyed(function (sprite: Sprite) {
})
And you would set it up right after you create the sprite in the code.
Now the third option is a bit more complicated, but I think this is actually the one you are looking for. To check if a certain sprite has the flag “destroyed” set to true, we can use this if statement here:
if (mySprite.flags & 2) {
}
“mySprite” being whichever sprite you want to check, of course. I just use it as an example because it’s the default sprite variable in block code.
A very long explanation of how that works...
Sprites all have this variable attached to them called “flags”. It’s a single variable, and every bit (1 or 0) is a different true/false “flag” that tells the physics and rendering engine different stuff about that sprite.
If anyone was wondering, this is where all those flags like “invisible”, “bounce on wall”, “ghost” and “relative to camera” are stored, all in a single variable!
So anyways, there is a flag for “destroyed” in there somewhere, and the real trick is figuring out how to check if a certain bit is “flipped” (1, aka true). We do this using the “&” bit operator.
The & operator outputs a number much like how the “+” operator does, but instead of adding two numbers together it “ands” them, outputting a number where each bit is flipped to 1 only if that bit is 1 in both numbers!
For example: 00110011 & 00101000 = 00100000 because only the 6th bit is flipped in both numbers! (At least I think that’s what you would say, because bit order is right to left, so it’s called the 6th bit and not the 3rd… I think…)
So to check if a certain bit is flipped we can “&” the “flags” variable with another number that only has the “destroyed” bit flipped. After a bit of digging into code, it turns out that the “destroyed” flag is the 2nd bit in the flags variable, corresponding to a value of 2!
(You can find out other flag values by writing “sprites.Flag.“ and then scrolling through the list that pops up of suggested flags. Then you can click on one to auto-fill it and hover your cursor over the variable to see what number it is. Because of the way binary works, each number is a power of 2, representing a single bit flipped!)
So yeah, you “and” the flag value with 2 and if the number that comes out is not 0, that means the “destroyed” flag is flipped, and the sprite is destroyed!
Oh, and we don’t have to use “ != 0 “ or anything because any number in an If statement that isn’t 0 counts as “true”.
P.S. All the issues I solve for people here are always using blocks, so it’s really refreshing to use JavaScript for once! You can do so much more in Makecode when you aren’t using blocks!