gemjock
gemjock

Reputation: 3

how to connect a number variable to dynamic text in actionscript 3.0?

i know this might be simple but i have been searching everywhere for a fix but i just cannot find it! i want to make something like a health #, so when you press whatever button the dynamic text # will go up or down. on my test project i have two layers, the first with the following code

var hp:Number = 100;
health.text = String hp;

hp being the variable, and health being the dynamic text. then i have the next layer with the button with:

function button(e:MouseEvent):void
{
hp -= 10;
}

without that second chunk of code, the dynamic text will appear, but once that is added it will disappear and the button is function-less. how do i make this work??? once again sorry if this is a dumb question, i'm just very stumped.

Upvotes: 0

Views: 99

Answers (2)

Aaron Beall
Aaron Beall

Reputation: 52133

The accepted answer is good, but I wanted to point out that your original code was actually very close to being correct, you just needed parenthesis:

health.text = String(hp);

For most objects String(object) and object.toString() has the same effect, except that object.toString() throws an error if object is null (which could be desirable or undesirable, depending on what you expect it to do).

Upvotes: 2

Daniil Subbotin
Daniil Subbotin

Reputation: 6708

This is not correct:

health.text = String hp;

use:

health.text = hp.toString();

and:

function button(e:MouseEvent):void
{
    hp -= 10;
    health.text = hp.toString();
}

Upvotes: 0

Related Questions