How do I Create a Delay In a Loop

0 favourites
  • 9 posts
From the Asset Store
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies. For use in game and custom works.
  • Hello,

    I have an array:

    For "x" from 0 to 100

    For "y" from 0 to 100

    Condition: Is overlapping "object"

    Action: Create object at loopindex("x"), loopindex("y")

    In a nutshell, I have a randomly generated shape that I spawn at a location. I find out the boundaries of this shape and then run an arrray at the same coordinates of that shape. It basically spawns a sprite on a 32 grid snap wherever the object is overlapping with the shape within that boundary.

    My problem is sometimes these shapes can be as may as 600 sprites. When the loop runs, spawning all 600 in one tick freezes the screen until it is done. Currently I have this function running during the game. I want to eliminate the freezing whenever these groups of sprites get generated. I tried put in a "wait", but it doesn't same to wait in between creating each sprite. Maybe someone else has a better idea how to spawn these sprites so that the screen doesn't freeze.


  • 'wait loopindex * 0.01sec' will introduce a 0.01 second lag between each repetition of the loop.

    Make sure you put the wait function in front of the other actions in the loop.

  • Hello,

    So I tired the delay in the loop above all actions, did not stop the screen freeze at all.

    Any other suggestion on how I can spawn so many sprites without overloading the CPU?

    Would appreciate any help. If I can get this accomplished it is a pretty major milestone in getting to the next phase of my game done.


    Actually I lied. I have managed to get it to work. I did it differently though. I put a 0.01 sec delay after checking for collision over top of the shape and "destroy" if not part of the loop. Then put another one after disabling collisions on the node.

  • Hi tried something similar, but I have a funny effect...

    This is the "wrong" situation, that is without the wait:

    It does spawn in the right direction, but in the wrong number:

    If I put the wait as first action (just don't look at the txtdebug, it acts the same way without), like this:

    It spawns the correct number of sprites, but in the totally wrong direction!

    I know that I am missing something very obvious, but I don't get what...

  • Any idea somebody?

  • Jeff Skyrunner: You should post your capx in a topic of your own.

    The actual informations you are giving are not enough at all to help explaining what is going on.

    You mention a wrong number of instances being spawned, that apparently has to do with the value of iNumLaser, since it is used as boundary in your loop, but we don't know where it is coming from.

    And when you delay, you are actually using the wrong "iTargetX" value, since you delayed the command, but the actual ShipSelected coordinates used is not delayed.

    Either save the actual coordinates to use with the same time offset (like in an array in wich you would push coordinates, and read them when the wait command has finished kicking in.)

    Maydie: Unfortunately, I'm afraid you can't really prevent the freeze caused by numerous instances being created/destroyed in a single tick.

    A way to cheat, is to have the spawn occur over several ticks instead of a single one, but I'm very unclear on what you are exactly attempting to do in the first place;, so I'm not sure this would fit.

    Nevertheless, it is a bad design in the first place to destroy/Create a lot of instances in a single tick, and you should modify this idea first in order to be able to implement something more appropriate.

  • Kyatric

    Sorry, I thought that being the same problem it would be better to use the same thread: my bad.

    Btw, thank you very much: you point me in the right direction! The error in the delay was that I was using local variable (iTargetX) instead of global ones (the target is always the same, so no need for an array).

    iNumLaser is correct: the "error" was just that being spawn all together, the sprites were all overlapping.

    Many thanks and sorry again for the error of using the same thread.

  • Maydie can you have the sprites invisible and turn them visible when needed? Or have them off screen and set their location? I don't think spawning 600 sprites at once is a good idea

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Jeff Skyrunner: It is not exactly the same problem.

    It has to do with loop and using the "wait" command. But the OPs issue is about spawning a massive lot of instances that freezes the execution when done in a single tick.

    In that, you issue was pretty different.

    Nevertheless, nice to know you manage in fixing your issue thank to this topic in the end.

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