Copy link to clipboard
Copied
I have a script for a counter that I increment from a button,
the script is:
var MyCounter:Number = 0;
Inside a button script:
.....bla bla ...
MyCounter = MyCounter + 1;
MyText.text = MyCounter.toString();
When I restart MyCounter from 0
I try:
var MyCounter:Number = 0;
if MyCounter was 4 it follow with 5 and so on.
Which is the syntax to reset the var in as3?
Thanks.
To reset the value to zero you would just need to assign it a value of zero.
MyCounter = 0;
What you just wrote makes it look like you declared the same variable again instead.
Copy link to clipboard
Copied
To reset the value to zero you would just need to assign it a value of zero.
MyCounter = 0;
What you just wrote makes it look like you declared the same variable again instead.
Copy link to clipboard
Copied
The compiler is telling you the variable already exists so you shouldn't use 'var' or a type (Number) again to try to create it, you should just assign a value.
If you're using timeline scripts and you're doing something like hitting frame 1 and are getting errors there because you decalred them, you'll have to sniff for existence.
e.g.
if (MyCounter) { MyCounter = 0; } // exists already, just reset
else { var MyCounter:Number = 0; } // doesn't exist, create
Happens a lot when people return to instantiation frames. If you have a lot of variables you instantiate on that frame (if that's even the case) it's better just not to return back to that frame. Make a function to clear your variables and don't go back to that frame.
function Reset():void
{
MyCounter = 0;
// reset other stuff
}
Copy link to clipboard
Copied
Thanks you both Sinious and Ned,
of course ... the August sun had made me make a mistake placing my original script "MyCounter = 0;" in the wrong place.
Thanks again!
Copy link to clipboard
Copied
You're welcome and good luck!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now