Skip to main content
Jhon Carlo
Inspiring
August 5, 2013
Answered

Reset a numeric variable in As3

  • August 5, 2013
  • 4 replies
  • 2871 views

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.

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer Ned Murphy

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.

4 replies

sinious
Legend
August 5, 2013

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

}

Jhon Carlo
Inspiring
August 5, 2013

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!

sinious
Legend
August 6, 2013

You're welcome and good luck!

Ned Murphy
Ned MurphyCorrect answer
Legend
August 5, 2013

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.