8 Replies Latest reply on Jul 23, 2016 7:54 PM by dounsabah

    How to loop x amount of times

    shadowfax007 Level 1

      Hello all!

       

      I've got a symbol that I want to loop 3 times on the main stage.

       

      I think I've figured some of this out but don't know how to write the code (I'm working on learning!), or if there is a better way :-)

       

      I was thinking of putting somethiing similar to the code below in the "onComplete" action of the symbol I want to loop (I know the code is wrong but I think it is a bit close):

       

      var l_count = (0+1);

      if (l_count <= 3) {

                sym.play();

                sym.setVariable("l_count", $l_count + 1)

      } else {

                sym.stop();

      }

       

       

      Is this close, or is there a better way to go about this?

       

      thanks for your help!

       

      James

        • 1. Re: How to loop x amount of times
          efru12 Level 1

          Not sure if you figured this out, but this is what I did :

           

          On the last frame of the timeline, add a trigger and add the following code to it :

           

          if(!sym.loop) {

          sym.loop = 1;

          }

          sym.loop += 1;

          if(sym.loop <= 3) {

          sym.play(0);

          }

           

           

          Now, this loops the timeline 3 times and at the end of the 3rd playthough, stops on the last frame.  If you want it to loop 5 times for example, change the bolded "3" to a 5.

          1 person found this helpful
          • 2. Re: How to loop x amount of times
            Alce_uk Level 1

            To your Stage.compositionReady function add:


            // Add a numLoops variable to global storage
            sym
            .setVariable("numLoops", 0);


            At the end of your animation via the Timelines actions, create an event to capture that variable, increment, and replay the animation if applicable like so:


            // capture numLoops variable and increment the counter
            var numLoopsHolder = sym.getVariable("numLoops");
            numLoopsHolder
            = numLoopsHolder + 1;

            // set new numLoops variable back to global storage
            sym
            .setVariable("numLoops", numLoopsHolder);

            if (numLoopsHolder <= 2) {
              
            // replay scene until 3 iterations are complete
              sym
            .play(0);
            }


            This will get it looping 3 times, but you still need to add a stop check at the desired timeline location via:

             

            // capture numLoops variable to check for 3 loops (ie. 0, 1, 2 = 3 loops)
            var numLoopsHolder = sym.getVariable("numLoops");

            if (numLoopsHolder >= 2) {
              
            // we've completed 3 loops now stop here
              sym
            .stop();
            }

            1 person found this helpful
            • 3. Re: How to loop x amount of times
              jenniferv63495149

              Thank you for the comment Alce_uk. The code is perfect for what I needed, except I'd like for the animation to loop once, then stop at .5 seconds before the end of the animation.

              I used your code at the end, noting the timeline location to stop, but can't seem to make it work? Advice?

              • 4. Re: How to loop x amount of times
                Alce_uk Level 1

                Hi Jennifer. Sorry for late reply. Have you managed to do your animation with loop and stop at 5 seconds? If not, I can try to help you.

                • 5. Re: How to loop x amount of times
                  nickc93085310

                  Hi Alce_uk

                   

                  I've followed your instructions but it's not working for me. I can get it to stop at the desired frame – but it won't loop.

                   

                  I've added to Stage CompositionReady function:

                  sym.setVariable("numLoops", 3);

                   

                  On the frame top stop at I've put:

                  var numLoopsHolder = sym.getVariable("numLoops");

                  if (numLoopsHolder >= 2) {

                    sym.stop();

                  }

                   

                  On last frame I've put:

                   

                  // capture numLoops variable and increment the counter

                  var numLoopsHolder = sym.getVariable("numLoops");

                  numLoopsHolder = numLoopsHolder + 1;

                   

                  // set new numLoops variable back to global storage

                  sym.setVariable("numLoops", numLoopsHolder);

                   

                  if (numLoopsHolder <= 2) {

                     // replay scene until 3 iterations are complete

                    sym.play(0);

                  }

                   

                  Is there  something wrong?

                  thanks in advance

                  • 6. Re: How to loop x amount of times
                    Alce_uk Level 1

                    Hi Nick,

                     

                    first of all:

                     

                    Stage CompositionReady function

                    sym.setVariable("numLoops", 3); -> instead "3" always should be "0".


                    Take a look at the example here:
                    www.alce.co.uk/edge/example.zip


                    Preview how it works:
                    http://www.alce.co.uk/edge/example/example.html

                     

                    I hope that would make sense to you.

                     

                    Regards,

                    Andrius

                    • 7. Re: How to loop x amount of times
                      nickc93085310 Level 1

                      Now it works. Thank you!

                      • 8. Re: How to loop x amount of times
                        dounsabah

                        Hello, Just a little Thank you and Acknowledgement for your answer above Alce_uk. I was trying to create a counter to count dropped items and when a certain number of items have dropped a message will show. I am new to code and couldn't figure it out until I found your your simple and clear example above. It helped me get mine working so thanks. For anyone interested in counting dropped items here is my code.

                         

                         

                        In the compositionReady event Set the global counter:

                         

                        sym.setVariable("dropped", 0);

                         

                        In the mouseover event of each droppable item count the drops and check if count number reached:

                         

                        var dropCount = sym.getVariable("dropped");

                         

                        sym.$("dragObject").draggable({opacity: 0.35, revert:"invalid"});

                         

                        sym.$("dropTarget").droppable({

                          accept: sym.$("dragObject"),

                             drop: function() {

                               dropCount = dropCount + 1;

                               sym.setVariable("dropped", dropCount);

                         

                                  if (dropCount == 9) {

                                  sym.play('done');

                                  }

                         

                          }});