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.

1
  • For your text, you can do : health.text = String(hp); . Commented Feb 15, 2015 at 11:41

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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();
}

7 Comments

it once again works up until putting the second code on the button. after putting that on the dynamic text disapears during testing it
Please attach FLA file.
You haven't registered an event handler. Above function "button" You must add this line button.addEventListener(MouseEvent.CLICK, buttonHandler); and rename a function "button" to buttonHandler.
awesome!! thank you so much!! just as a follow up question, what is the code to cap it off at a specific number? (im in the process of finally switching from 2.0 from 3.0, a lot of learning ha)
Sorry. I don't understand Your question. Can you write more clearly?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.