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

  • 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.

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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?

  • Ah, found it. Pick Random instance

  • Ah, found it. Pick Random instance

    hahaha yes... pick random instance. Sorry, too late. I was on a meeting.

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)