Skip to main content
carlp33822801
Inspiring
November 6, 2017
Answered

HTML5 canvas: how do I create a private/local variable in Actionscript?

  • November 6, 2017
  • 1 reply
  • 576 views

up vote0down votefavorite

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; //reset button
   }
}

Call:

this.Foo_Btn.addEventListener("click", animateTarget.bind(this, this.Foo_target, 34));
This topic has been closed for replies.
Correct answer carlp33822801

So I got the sullotion from a friend:

function animateTarget(target, rwFrame) {

     if(!target.clicked)

     {

          target.gotoAndPlay(2);

          target.clicked = true;

     }

     else

     {

     target.gotoAndPlay(rwFrame);

     }

}

1 reply

Legend
November 6, 2017

You prefix "count" with "this." exactly once at the beginning of the function, and then never again. No wonder it's not working correctly. Put this. in front of all of them. And get rid of "var count;" at the top. That serves no purpose that I can see.

Also, using "count" as the name of a Boolean is terrible naming. That sounds like a variable that holds a number. Y'know, a variable that counts things.

And this line...

if (!count == true) {

...is the sort of code that gives people aneurysms. Swap your logic around and just do:

if (count) {

I'm not even going to ask why you're prefixing your function arguments with dollar signs. It makes it look like you're using jQuery, even though you aren't.

carlp33822801
Inspiring
November 7, 2017

Thanks for answer:

So I tried your suggestion:


function animateTarget(target, rwFrame) { 
   this.clicked = false
   if(!clicked) {
        target.gotoAndPlay(
2);
       
this.clicked=true;
    }
else {
        target.gotoAndPlay(rwFrame);
//playback from a different frame
       
this.clicked=false; //reset button
    }
}

this only gives a ”Uncaught ReferenceError: clicked is not defined”

Then I tried using var only inside function:

function animateTarget(target, rwFrame) { 
   var clicked = false
   if(!clicked) {
        target.gotoAndPlay(
2);
        clicked=
true;
    }
else {
        target.gotoAndPlay(rwFrame);
//playback from a different frame
        clicked=
false; //reset button
    }
}

This only gives ”Clicked = false” each time the function is fired (the change to true is not stored for each object).

So I need to find a way to change var clicked for each object using the function.

carlp33822801
carlp33822801AuthorCorrect answer
Inspiring
November 7, 2017

So I got the sullotion from a friend:

function animateTarget(target, rwFrame) {

     if(!target.clicked)

     {

          target.gotoAndPlay(2);

          target.clicked = true;

     }

     else

     {

     target.gotoAndPlay(rwFrame);

     }

}