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

How to make the counter never go below Zero or how to count negative number

Participant ,
May 05, 2015 May 05, 2015

Copy link to clipboard

Copied

So I have this counter that I made which basically counts points but it also subtract points. sometimes in certain conditions the counter goes below zero and then I get a weird number 4294967266. my goal is to make counter remember negative numbers but if it's too defunct at least to stop at Zero.  Any Ideas?

here is my counter:

//Scoreboard starts here:

var score: uint;

score = 100;

scoreBoard.text = "" + score.toString();

button_10.addEventListener(MouseEvent.CLICK, on_press1);

button_11.addEventListener(MouseEvent.CLICK, on_press2);

//Positive button adding points

function on_press1(event: MouseEvent): void {

  gotoAndStop(2);

  score += 100;

  scoreBoard.text = "" + score.toString();

  gotoAndPlay(61);

}

//Negative button subtracting points

function on_press2(event: MouseEvent): void {

  score += -20;

  scoreBoard.text = "" + score.toString();

}

//Scoreboard ends here:

TOPICS
ActionScript

Views

4.3K

Translate

Translate

Report

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

Participant , May 05, 2015 May 05, 2015

I've got it! the variable should be int and not uint. like so


var score: int;

Votes

Translate

Translate
Community Expert ,
May 05, 2015 May 05, 2015

Copy link to clipboard

Copied

Just have an if statement which checks to see if it is less than or equal to zero - if so... override the display to 0 no matter what the actual number is or force the number.

Votes

Translate

Translate

Report

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
Participant ,
May 05, 2015 May 05, 2015

Copy link to clipboard

Copied

that would be easy but every time it goes below zero it turns into this number 4294967266 which is obviously not smaller than zero and not a negative zero. who to avoid  this number?

Votes

Translate

Translate

Report

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
Community Expert ,
May 05, 2015 May 05, 2015

Copy link to clipboard

Copied

Always the same number? Perhaps there is something else going on in the code? You could look for that number and set a breakpoint for when it occurs to inspect what is going on.

Votes

Translate

Translate

Report

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
Participant ,
May 05, 2015 May 05, 2015

Copy link to clipboard

Copied

yup it's always the same number when it goes below zero. However it does the math correctly when I click on the plus button it will go to normal numbers after it goes back to above zero. I'd upload the file for you to look if I knew how. but here is the script if you can look at it and figures it out.

import flash.text.TextFormat;

import flash.text.TextField;

var score: uint;

score = 0;

scoreBoard.text = "" + score.toString();

stop();

//Scoreboard starts here:

button_10.addEventListener(MouseEvent.CLICK, on_press1);

button_11.addEventListener(MouseEvent.CLICK, on_press2);

//Positive button adding points

function on_press1(event: MouseEvent): void {

  score += 100;

  scoreBoard.text = "" + score.toString();

}


//Negative button subtracting points

function on_press2(event: MouseEvent): void {

  score -= 20;

  scoreBoard.text = score.toString();

}

//Scoreboard ends here:

Votes

Translate

Translate

Report

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
Participant ,
May 05, 2015 May 05, 2015

Copy link to clipboard

Copied

LATEST

I've got it! the variable should be int and not uint. like so


var score: int;

Votes

Translate

Translate

Report

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