Skip to main content
May 24, 2014
Question

What is "toString"?

  • May 24, 2014
  • 1 reply
  • 334 views

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 has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
May 24, 2014

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

May 25, 2014

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?

kglad
Community Expert
Community Expert
May 25, 2014

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.