-
ForEach Problem
Yet again, ForEach is failing me :(
I have enemies, and they die via bombs, so what I'm trying to do is count how many enemies each bomb kills so I can correctly give bonuses for Double Kills and Triple Kills.
I do a ForEach loop through the enemies, they get the proper health subtractions, then I've tried SEVERAL ways to get the count, none of them working.
1
First I tried putting in the ForEach loop, after health subtraction, If On ForEach Loop and Health of Enemy <= 0, Add 1 to kills (Global Value). I've also tried setting kills to kills+1 to no avail. I have determined that fastloops creating particles wasn't interfering. Oh and to give more info, kills was Always set to 0 in the first event, then I read the value of kills after the ForEach loop to determine what to do.
To see what was going on, if kills > 0 I would add the value of kills to a visible List. What I observed was SOMETIMES it would work, but a lot of times instead of saying 2, it would do 1 then 1. But the health subtraction occured in 1 frame. There's no possibility it could occur in multiple frames because I remove the bomb object that the condition is dependent on to subtract health. Also, the foreach loop was properly destroying the enemies in 1 frame as I observed via pausing.
2
A second idea I had was to count the enemies. Let's use the global value kills again. In my first event I set kills to the number of enemies. Right before I need kills, after all health determination and destruction, I would set kills to kills minus the number of enemies. This SHOULD have yielded the number killed, but it was always giving 0.
I have no idea what to do, if anyone can help me, thank you very much. I've spent too much time on such a silly problem :'(
Thank you
-Eliyahu
-
Re: ForEach Problem
You can have a foreach loop call another foreach loop (nested foreach loop). So you can loop through the bombs then loop through the enemies.
Also make sure you are using the loop with object selection, so mmf know which to pick, rather than the foreach loop without.
-
Re: ForEach Problem
Perhaps use the Select Object?
It can count the number of objects of a type that match the conditions.
-
Re: ForEach Problem
A nested ForEach loop wouldn't solve any problems, and I am using the object selection loop.
I cannot use Select Object, Dynasoft, this needs to be able to run in flash.
RickyRombo helped me and we discovered that NObjects("enemy") was only being updated every frame, not when Destroy was called, so that's why my second idea didn't work.
So I fixed it to keep track of the last frame's count, then compare it to the new frame's count, and determine how many kills occured.
The problem is still that I get 1 then 1 instead of 2. This must be a ForEach problem that its effects are not taking place within the single frame they should. Is there any solution to this bug?
EDIT: I just fixed it, it was really weird and I have no idea why it works now. I moved the action that was destroying the bomb. It looks like it was staying alive for 2 frames, but I considered the order of events and that should not have happened. This was a really weird problem.... But at least it works now! :D
Thanks all