Enemy movement....how to use For Each?

I have an Enemy “GreenZol” which when hit with a Wooden Sword splits into two smaller enemies called “GreenGelFromZol”. I then delete the GreenZol enemy and begin to control the movement of the GreenGelFromZol enemies with a timer and direction variable using permanent forces, changing their direction every .75 seconds.

The problem I’m having is when there are two or more GreenZol enemies on screen and I hit the first one with the Wooden Sword, the GreenGelFromZol enemies appear and behave exactly how I want them to. However, when I leave the GreenGelFromZol enemies alone and go hit the other GreenZol enemy with the sword, it creates the two GreenGelFromZol enemies as intended, but they don’t move.

The result I want is for the GreenGelFromZol enemies from the second GreenZol enemy to move just like they did from the first GreenZol enemy. I assume I need a For Each loop somewhere in my code to make this happen, but where I have it in my code doesn’t seem to be working. Here is a code snippet:

Can someone please tell me where I’m going wrong in this code to get my intended result? Thank you!

EDIT: Here is the part of the code above the events I already posted, just in case it is relevant.

A “trigger once” inside a “for each” has unexpected results. I think the “for each” is unnecessary here.
The 4 directions events can be move as sub events of the collision event to simplify.

When I remove the Trigger Once from the For Each event, I get the result of the enemies (GreenGelFromZol) moving very fast and erratically, like they’re event is being executed every frame which results in the permanent forces making them move out of control. When I remove the For Each event AND the Trigger Once condition, I get the same result described in my original post: The first set of GreenGelFromZol enemies will move as I want them to, but if I leave them and go hit the other GreenZol enemy, the GreenGelFromZol enemies for it appear, but do not move at all.

From playing around with it, I identified the Trigger Once as the root of the problem, but I’m unsure of how to get this intended result for all of the GreenGelFromZol enemies on the screen without removing the Trigger Once condition, which then results in the permanent forces going crazy.

I can’t have the Trigger Once there, I understand that much…but how would I accomplish what I want to happen once I remove that condition? Thanks!

If the 2 characters behave in the same way, you could either put them in a group and move them using the group name and the same events or put them in the one object and change the animation when they split. If you used this technique you could change the animation of the existing character and just add a second object with its animation set to the smaller animation. You could either check the animation or use an object variable to check which version it is.

I’ll give that method a try and let you know how it works. Thanks!

1 Like