[SOLVED] For Each Instance of an Object Not Working...?

Hi, I’m trying to test a collision between a player shield (like Mario star power) and bad guys. I’m using “repeat for each instance” of the enemy and the shield eliminates bad guys as it should…as long as the shield isn’t overlapping two bad guys at the same time. As of now the shield must kill one bad guy, leave his collision space, and the shield is ready to collide with another bad guy. If the shield remains in collision with the bad guy, it will not eliminate the next bad guy. I’ve tried the “repeat for each instance” in various places.

I could solve the problem by deleting the bad guy and respawning a new sprite complete the death sequence, but I’d like to keep all the animation within one sprite. And I feel like I’m just using the “repeat for each instance” command wrong and would like to learn the proper way. How would you code this type of collision where the sprite remains in contact and still detects new collisions with different instances of a sprite?

The “repeat for each instance” includes a “trigger once” for the actions list, being that I don’t want them repeated multiple times for a single sprite. I’ve tried nestling the “trigger once” both above and below the “repeat for each instance” command with no luck.

Thank you in advance for the help!

Hi @LittleCityGames, can you provide screen snips of the events? It’s hard to picture what you’ve done going by what you’ve described. We may have one idea of what you’ve coded, and the reality could be quite different.

Hi, thanks for the quick reply! Here’s a simplified mockup as an example. I’ve tried re-ordering the command for “each instance” and “trigger once” with no luck.

Note: the “Player” object in my mockup game is actually a “Shield” object in the original…I just mislabeled in this simplified recreation. So assume the player is invincible and trying to hit as many bad guys as it can.

I must use some form of a “trigger once” command because in the actual games I’m triggering several actions such as audio.

Thank you!

You cannot use Trigger Once within a For Each instance.

It behaves as it is intended to, but not as you’d expect. It triggers once, as in, only the first instance will have the events occur on it.

You have two options:

  1. Establish a “StopandMove” object instance that you use to act as a trigger once (Set it to 0 upon collision, require it to be 0 in the “For each”, set it to 1 as the last action of for each event).
  2. Try playing around with the “Trigger Once For Each” extension. I’ve not tested this and cannot provide guidance on how/if it works.

Ah that was extremely helpful! I didn’t realize “Trigger Once” worked that way. I ended up using a boolean object variable on the bad guy. When checking for the collision, if the “Alive” variable was still true, it would carry out the actions. At the end of the actions I set the “Alive” boolean to false. Easy way to collision test multiple instances separately. Thank you!