user2421975
user2421975

Reputation: 197

How to raise or decrease a display number variable in AS3 with a condition?

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 !!!

Upvotes: 0

Views: 165

Answers (2)

Bennett Keller
Bennett Keller

Reputation: 1714

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

Upvotes: 0

Paul Facklam
Paul Facklam

Reputation: 1633

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

Upvotes: 1

Related Questions