Search code examples
actionscript-3variablesdynamic-text

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.


Solution

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