Skip to main content
deborahb44958436
Inspiring
May 5, 2015
Answered

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

  • May 5, 2015
  • 1 reply
  • 5072 views

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:

This topic has been closed for replies.
Correct answer deborahb44958436

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:


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


var score: int;

1 reply

Joseph Labrecque
Community Expert
Community Expert
May 5, 2015

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.

deborahb44958436
Inspiring
May 5, 2015

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?

deborahb44958436
deborahb44958436AuthorCorrect answer
Inspiring
May 5, 2015

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:


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


var score: int;