How do I make it so when all enemies are destroyed, a door opens in the tilemap?

This issue has been bugging me for what feels like years, and is completely haulting the progress of my game. Right now, I have it set up where when an enemy spawns, a variable goes up, and when one dies, the variable goes down. And when the variable reaches 0, the door opens. For some god-forsaken reason, the variable is refusing to budge when an enemy dies? Can someone help me?

Heres the link for the (very early) build of my game, any help is appreaciated, thanks!

4 Likes

Looks like you are double counting your enemies; you’re changing enemyCount by 1 both inside spawnEvilCat and outside the function when you call it.

Instead, consider using the “array of all sprite of kind” block. You can check the length of the array it returns and if it’s 0, there are no Sprites of that kind left undestroyed

1 Like

Thanks for the response! Is there a way I can get it so it will repeatedly check the array, and once it confirms that there are zero enemies, it triggers the objects once, and then breaks?

1 Like

You could make it check every single update with a forever block or only make it check with a on sprite destroyed block to save space and time

instead of only one variable for both of them, make two variables. one called “enemyCount” and one called “enemyKO”. make it so that enemyCount is equal to the number of enemies in the room, and have enemyKO go up by 1 whenever an enemy is defeated. when enemyKO = enemycount, set enemyKO to 0 and open a door.

1 Like

Tried this, but for some reason the door still wont open. It’s like another enemy randomly spawns out of bounds or something.

hmm… try opening the door and THEN setting enemyKO to 0?

1 Like

The code just isn’t triggering, all enemies are dead, enemy ko equals enemycount, but it still doesn’t trigger.

1 Like

Try putting it in an on game update block that constantly checks the two values, and replaces a tile with another when they’re equal and reverts it back when they’re not.

I think you have the right idea with the on destroyed block:
Screenshot 2024-01-10 at 1.22.31 PM

But, because you keep going back and forth between kind Enemy and kind Invincible, it’s throwing things off. See if it works if you remove those for a bit, then see what that tells you about how you need to account for those changes.

Try adding something like this and see if that helps you debug:

2 Likes

You should probably adjust this to suit your game but this seems like it should work

1 Like

How come some number blocks require this: image to work but others don’t?

2 Likes