How do I put a delay in a for each loop?

Just started using Construct 2? Post your questions here

Post » Wed Feb 22, 2017 7:10 pm

Hello all,

I am trying to do a Tower Defence game and I want to add an effect at the end of the levels if the player fails. I want the towers to explode one by one with a short delay in between.

I tried a "for each" loop, where each tower spawns an explosion and gets destroyed followed by a "wait(1)" but that didn't work. The explosions all happen at once.

Any idea how to achieve the desired effect? Many thanks.
B
14
S
3
Posts: 29
Reputation: 870

Post » Wed Feb 22, 2017 7:27 pm

Wait doesn't work on loops.
You should use an "Every X seconds" event, then you pick one instance of your towers family and apply a destroy action to it.
Lead developer at Octeto Studios

Visit our portfolio at: http://www.octetostudios.com/ :)

Facebook || Twitter || Youtube
B
37
S
7
G
5
Posts: 217
Reputation: 5,614

Post » Wed Feb 22, 2017 7:56 pm

Thanks GamerGon. How do I pick one instance of my tower? Is there an easy way or do I have to cycle through all objects until I found an instance of my Tower object?
B
14
S
3
Posts: 29
Reputation: 870

Post » Wed Feb 22, 2017 8:39 pm

Ah, found it. Pick Random instance :D
B
14
S
3
Posts: 29
Reputation: 870

Post » Wed Feb 22, 2017 10:44 pm

Taweel wrote:Ah, found it. Pick Random instance :D


hahaha yes... pick random instance. Sorry, too late. I was on a meeting.
Lead developer at Octeto Studios

Visit our portfolio at: http://www.octetostudios.com/ :)

Facebook || Twitter || Youtube
B
37
S
7
G
5
Posts: 217
Reputation: 5,614

Post » Mon Feb 27, 2017 3:59 pm

Another way of doing it: set wait to 1+loopindex*1. This method skips some objects if there are too many so I have a destruction array.

Destruction Array:
When the game ends do an ordered loop for each tower(I usually choose tower.y ascending) and write each tower's UID and time of destruction.
set destructionarray(loopindex,0): Tower.UID
set destuctionarray(loopindex,1): time+1+loopindex

after creating the array you can:
Compare at XY (0,1): whever this value is smaller than time(the system expression) destroy the tower with UID destructionarray(0,0) and delete this array value. When your array has 0 values the game ends.
B
13
S
2
Posts: 6
Reputation: 637


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 0 guests