-1

I'm using a variable to create a toggle-effect on a function. The only problem is that this code changes the variable globally, and not locally for each object using the function. I know what I have to do, just not how :)

Function:

var count;

function animateTarget($target, $rwFrame) {

        this.count = false;

        if(!count == true)
        {
            $target.gotoAndPlay(2);
            count=true;
        }
        else 
        {
            $target.gotoAndPlay($rwFrame); //playback from a different frame
            count=false;
        }
}

Call:

this.Foo_Btn.addEventListener("click", animateTarget.bind(this, this.Foo_target, 34));
Carl Papworth
  • 1,282
  • 2
  • 16
  • 35
  • Did not downvote but... _"and not locally for each object using the function"_ sounds like there's something else you're not telling us... **(1)** What is _"each object"_ exactly, a Class object? a MovieClip object? an external loaded SWF object? **(2)** Does said object(s) have its own local / internal variable called count to be updated? **(3)** Could an `array` that simply holds the true/false _"count"_ states for each of your objects not achieve same thing? – VC.One Nov 08 '17 at 16:09

1 Answers1

0

Actually, I got the answer from a friend (here with some updated variable names). Since it's JS, it reads the "clicked" as false, even though this is not stated. And by binding it to the object targetit will check for each separate object that uses this function.

var clicked;
    function animateTarget(target, rwFrame) {

         if(!target.clicked)
         {
              target.gotoAndPlay(2);
              target.clicked = true;
         }
         else
         {
         target.gotoAndPlay(rwFrame);
         }
    }
Carl Papworth
  • 1,282
  • 2
  • 16
  • 35