/AS3/ RPG - Discount numbers to Variables

Subscribe to /AS3/ RPG - Discount numbers to Variables 11 posts

avatar for LithiumP LithiumP 5 posts
Flag Post

var str:int = 1;
var hp:int = 15;
texthp.text = hp.toString(); //i have a dynamic text with an instance name of “texthp”

attack.addEventListener(MouseEvent.CLICK, playerattack);

function playerattack(Event:MouseEvent):void{
hp – str;
}

i want discount str to hp but this code doesn’t work.

 
avatar for clasher235 clasher235 586 posts
Flag Post

it’s not hp - str, it’s hp -= str.

 
avatar for LithiumP LithiumP 5 posts
Flag Post

still doesn’t work

 
avatar for Amibtious Amibtious 377 posts
Flag Post

Are you updating texthp to the new value of hp?

 
avatar for Khronosis Khronosis 130 posts
Flag Post

The hp variable is indeed going down (you could trace it to verify), but you’re not updating the text field to display the change.

 
avatar for Senekis93 Senekis93 4090 posts
Flag Post
Originally posted by Khronosis:

The hp variable is indeed going down (you could trace it to verify), but you’re not updating the text field to display the change.

The variable is never modified, as the assignment operator isn’t being used.
Tracing hp will return 15.

 
avatar for Khronosis Khronosis 130 posts
Flag Post
Originally posted by Senekis93:

The variable is never modified, as the assignment operator isn’t being used.

I meant it’s going down with clasher235’s solution.

 
avatar for LithiumP LithiumP 5 posts
Flag Post

here is the final code:

function playerattack(Event:MouseEvent):void{
hp -= str;
texthp.text = hp.toString(); // after doing hp -= str, refresh the text.
}

 
avatar for LithiumP LithiumP 5 posts
Flag Post

Thanks for your help!!

 
avatar for Dealmaster13 Dealmaster13 641 posts
Flag Post

Just remember that int and String are primitive types in AS3 and so have their data copied; not their reference

 
avatar for Draco18s Draco18s 6860 posts
Flag Post
Originally posted by Dealmaster13:

Just remember that int and String are primitive types in AS3 and so have their data copied; not their reference

On the other hand, if they were referenced, you wouldn’t get logical output when doing myText.text = myVar (you’d get a pointer value!)