3 Replies Latest reply on Nov 6, 2012 3:09 PM by TimJaramillo

    Conditional Basics

    edgarTL

      I thought I'd make a simple button toggle but it turns out I'm missing some basics...

       

      Here's the code in a click action applied to a button on the main stage

       

      var isPlaying = false;

       

      if(isPlaying == false){

            sym.getSymbol("windTurbine1").play();

            isPlaying = true;

      } else {

            sym.getSymbol("windTurbine1").stop();

            isPlaying = false;

      }

       

       

      It does play the symbol but does not toggle.  I've tried to use a boolean primitive and a boolean object - what it the scope of the code above?  Is the isPlaying a global variable scoped to the stage and can I just address it by name?

       

      Any insight or references is appreciated.

        • 1. Re: Conditional Basics
          TimJaramillo Level 4

          I think this is what's happening- since your code is on a click action, it runs everytime a click occurs, thus "isPlaying" is set to false each click.

           

          Instead of putting your code on the button, put all your code on Stage.compositionReady, then call the function from your button. Keeping all functional code in one place is also a better way to organize your file.

           

           

          // code on Stage.compositionReady

          sym.isPlaying = false;

           

          sym.onClick = function() {

               if(sym.isPlaying == false){

                     sym.getSymbol("windTurbine1").play();

                     sym.isPlaying = true;

               } else {

                     sym.getSymbol("windTurbine1").stop();

                     sym.isPlaying = false;

               }

          }

           

           

          // code on your button's click event

          sym.getComposition().getStage().onClick();

          • 2. Re: Conditional Basics
            edgarTL Level 1

            Tim,

             

            You Rock like Dokken!

             

            So, every time I code a button in Edge, the button code is actually "scoped" to the button and not the stage (sym)?

             

            Also, if I create the variable isPlaying on the stage, and I'm referenceing it from the stage - is the sym. still necessary for addressing or just good form or both?

             

            Finally, is console.log() the equivelant of trace() and can only be seen in a browser panel vs in a floating output window?  I'm also assuming not supported in all browsers...

             

            Thanks very much,

            • 3. Re: Conditional Basics
              TimJaramillo Level 4

              Haha, thanks edgar!

               

              In your case, since the code was on click of the button it was resetting the var each time, regardless of scope.

               

              Regarding using sym before the variable, it is necessary if you want to access the variable from nested symbols, etc. But if all your functional code is on Stage.compositionReady, you can do this:

               

              var isPlaying = false;

               

              sym.onClick = function() {

                   if(isPlaying == false){

                         sym.getSymbol("windTurbine1").play();

                        isPlaying = true;

                   } else {

                         sym.getSymbol("windTurbine1").stop();

                        isPlaying = false;

                   }

              }

               

               

              console.log() is supported in all browsers, and ya- it can only be seen in your browser's console log. If you want a floating output window, you can use alert();