Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Reset a numeric variable in As3

Contributor ,
Aug 05, 2013 Aug 05, 2013

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.

TOPICS
ActionScript
2.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Aug 05, 2013 Aug 05, 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.

Translate
LEGEND ,
Aug 05, 2013 Aug 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 05, 2013 Aug 05, 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

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 05, 2013 Aug 05, 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 06, 2013 Aug 06, 2013
LATEST

You're welcome and good luck!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines