Loop n number of times

Hi everyone. I found out about wick at processing day NYC and am now trying to play around with it. Ultimately I want to build a game that loops an animation a number of times based on user input. Assuming there is not already a tutorial for doing that, the first step is going to be just building animation and having it loop n number of times. Is the best way to do that just a for loop? I have added a for loop to the default code for the clip:

for (i = 0; i < 5; i++) {

but I don’t know what should go in the loop to make it run 5 times and then stop.


You can’t really use for/while, for the loop animation. Otherwise, it will crash. So, I use update event to do it instead.

Here's the code

this.anim_loop_count = 5; // The ammount of loops
this.anim_loop_iter = 0; // To play the loop again, set this to 0
this.anim_loop_playing = false; // To prevent gotoAndPlay Stuck when in update event

onEvent('update', function () {
    this.loop(1, 20); // this.loop(startFrame, endFrame)

this.loop = function (start, end) {
    if (this.anim_loop_iter < this.anim_loop_count) {
        if (this.currentFrameNumber === start && this.anim_loop_playing === false) {
            this.anim_loop_playing = true;
        else if (this.currentFrameNumber === end && this.anim_loop_playing === true) {
            this.anim_loop_playing = false;

Loop nth time 2-11-2020_7-35-54AM.wick (22.9 KB)

Thanks! Where do I insert that code? I clicked on the clip. There were two blue buttons (‘Break Apart’ and “Edit Timeline”) along with code. I clicked on the default code and added this code. Unfortunately it didn’t seem to limit the number of loops. I even tried to create a new clip within a frame just in case my original clip was messed up. That did not seem to make any difference.


Below “Edit Timeline”, below “Scripts”, click “Default”.
And you DON’T just insert the code, you’d need to put it where it belongs in the scope. And integrate my code with yours.
Or… Let me see your save file.

Thanks! I got it to work - this is super helpful.

One follow up question - is there an existing example that takes user input from frame 1 and uses it to control the number of loops in this code in frame 2?

You can save information between frames inside the project object like this:

project.value = 5;

And then calling project.value in the next frame instead of a regular number!

1 Like