Stop a 'Do every x seconds' loop ?

nikopol

  • Posts: 26
Hey all,

I tried stopping a 'do every ... seconds' loop by using the 'stop' command, but it doesn't seem to work.
Here is a screenshot of my behaviour.

Perhaps it only stop the 'if' block ?

stefan

  • *
  • Posts: 2263
the Stop block prevents the rest of the code being executed. The Do Every block cant be stopped on it's own, but instead you can put an IF statement inside the Do Every block, and only execute whatever is inside the IF state when a boolean is true.

Personally I never use the Do Every block, but use a Custom Event that repeats itself instead. This gives me more control such as pauzing, repeating and even change the time it waits before the next loop.

Here is a basic example:

When CreateEnemy happens
~ create enemy at ...
~ increment compteur by 1
~ if compteur < (Rounds * nbEnemies) //I asume the loop has to stop when compteur is the same as (Rounds * nbEnemies)?
~~ Do after 3 seconds
~~~ trigger event CreateEnemy

nikopol

  • Posts: 26
Allright ! Thanks for the 2 solutions ! I will do as you say, create a second custom event .

Btw, your channel is awesome ! You helped me a lot with all your vids, thanks.

stefan

  • *
  • Posts: 2263
Glad I can help ;)

daverave1212

  • Posts: 26
I know it's very late, but there's a way to stop a "do every" block, but not from Stencyl blocks.

When you do the "do every X seconds" block, it calls the function "runPeriodically(....)".
"runPeriodically" returns a TimedTask object.
You can set its "repeats" property to false when you want the loop to stop.

Code: [Select]
  var timedTask = runPeriodically(myMiliseconds, function(task) {
    // your code here will be repeated every "myMiliseconds" miliseconds
  });

When you want to stop it, simply say
Code: [Select]
  timedTask.repeats = false;

I haven't tested this myself but I think it should work.

« Last Edit: May 06, 2021, 06:33:12 am by daverave1212 »