Skip to main content
May 24, 2014 9:16 PM UTC
Question

What is "toString"?

So I have this piece of code which allows for a score to be displayed in my game

var plScore:int = 0;

         score_txt.text = plScore.toString();

It then updates by 1 when the player collects an item

plScore +=1;

     score_txt.text = plScore.toString();

It works fine however I want to know what "toString()" is and how it works in the context of this set of code as all I really know is that it allows for the numbers to be displayed as text in the level but I am unsure how it does it.

This topic is closed to new replies. Start a new post to keep the conversation going.

3 replies

kglad
Community Expert
Community Expert
May 24, 2014 9:41 PM UTC

it's just converting file type from (in your code, an int to a string).  casting does the same:

var plScore:int=0;

score_txt.text=String(plScore);

Translate
May 25, 2014 5:48 PM UTC

Ah ok thanks. Could you please briefly explain how "string" works int the context of this code, like how does it allow for the numbers to be displayed as text?

Translate
kglad
Community Expert
Community Expert
May 25, 2014 7:20 PM UTC

i don't understand what you are asking but maybe this will help.

you could try:

score_txt.text = plScore;

and read the ensuing error message.  perhaps that will help you understand why casting or using toString is needed.

or you may have a more profound question about why adobe engineers/developers chose to have textfields display strings only.  or maybe you don't understand 'typing' and need some help with a more basic understanding of programming principles.  like i stated in the first sentence, i don't understand what you're asking.

Translate