Check if sprite exists

Hello,
How can I check if a sprite exists on the screen?
I want to make a shooter game but with a limit to the speed the shots can be fired.
I thougt about using logic to see if the the sprite exists, so only one shoot can be fired untill it hits a target or it goes off screen but I dont know of a way to check this.
Anyone have some idea how to make this? Or some other sollution?
Thank you.

1 Like

The sprite utils extension (https://github.com/jwunderl/arcade-sprite-util) has a boolean block which checks if a sprite exists and it isn’t destroyed.

We do this one pretty frequently in shooter games. Great question! Take a look at this code:

In the event handler for the A button press, a projectile is only created if none exist. You can modify the logic statement to suit your needs, but that’s the idea:

  • Get a list of all sprites of a particular type.
  • Count them up.
  • See if there number on the screen allows us to add another sprite (or whatever action you like).

You could add an else clause there and play a sound or something to let the player know that they can’t shoot right now.

This code works because the set projectile creates a sprite that automatically destroys itself when it leaves the screen. If you’re not using the set projectile block, then that’s OK! Just add another block that sets auto destroy to ON, right after you create the sprite. You can find that block in the Sprites drawer of the toolbox.

1 Like

Thanks. That is exactly what I need.

1 Like