Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

What is "toString"?

Guest
May 24, 2014 May 24, 2014

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.

TOPICS
ActionScript
295
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2014 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 25, 2014 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 25, 2014 May 25, 2014
LATEST

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines