How to raise or decrease a display number variable

2019-09-02 03:48发布

问题:

I've got, in my Toolbar.as class, a simple variable (4-digit number) displayed in a dynamic text box, like that :

var number = 9999; 
useText.text = String(number); 
trace(number);

In my puzzle class I have a condition and I would like to decrease the number if it's true. How can I do that ?

For exemple, I've got, in my Puzzle.as class :

if (inv.containsItem("rock")) {
    toolbar.useText.text = "String(number)" - 100;
    }

But it doesn't work (I know that I have to change the "String(number)" - 100 but what should I put ?

Thx !!!

回答1:

I believe you are attempting to do: String(number - 100);

The is called a wrapper or casting, you are casting the number variable with the operation of - 100 to a String.

If you want to grab the current text in your field and use that:

useText.text = String( Number( useText.text ) - 100 );

This casts the current text to a Number, subtracts 100, then casts it back to a String



回答2:

You have to take the value of toolbar.useText.text, parse it to int, subtract 100 and assign to toolbar.useText.text.