How to Display Strings Using AS3

Written by mark stansberry
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Display Strings Using AS3
Create a sign for your virtual online store with AS3 strings (Jupiterimages/Brand X Pictures/Getty Images)

Displaying strings, variables that are used to hold text, with ActionScript 3.0 (AS3) allows you to include text messages in your flash movies. Knowing how to display text from strings will enable you to add user-friendly features such as instructions to your flash game. Displaying the string's contents (text) on the screen requires that you declare a string variable and a TextField. After assigning text to the string variable, you assign the string to the text property of the TextField. Then you display the textbox with AS3's add Child command.

Skill level:

Things you need

  • Adobe Flash Professional

Show MoreHide


  1. 1

    Write the first line of code for the declaration of your string variable called sign: var sign:String

  2. 2

    Write the second line of code to assign the word "OPEN" to the string variable you declared in line one: sign ="OPEN"

  3. 3

    Write the third line code for the declaration of a TextField(textbox) object: var outputword:TextField = new TextField ().

  4. 4

    Write the fourth line of code to assign the contents of the string, OPEN, to the text property of the TextField, outputword, you created in step two: outputWord.text = sign

  5. 5

    Write the fifth line of code to place the TextField, output Word, on the stage (the screen): add Child(outputWord)

  6. 6

    Write the sixth and seventh line of code to assign a position for the TextField, ouputWord, on the stage (the screen): outputWord.x = 250; outputWord.y = 250.

  7. 7

    Review the code you wrote (below) for syntax errors and correct functionality.

    var sign: String

    sign ="OPEN"

    var output Word:TextField = new TextField()

    outputWord.text = sign

    add Child(outputWord)

    outputWord.x = 250

    outputWord.y = 250

  8. 8

    Enter the code from Step 7 in the ActionScript editor in your flash file. After entering it, click the syntax checker button to check for syntax errors. Correct any typing or syntax errors that the compiler (error checker) reports. Next, select the "Test Movie" option from the "Control" menu to view the movie.

  9. 9

    Change the code so that the word displayed in the movie will be "CLOSED" and the position of the word will be displayed in the lower left right corner of the stage (coordinate point x=500, y=500). Check to see if your changes are correct by comparing your changes with the code listed below.

    var sign: String

    sign ="CLOSED"

    var output Word:TextField = new TextField()

    outputWord.text = sign

    add Child(outputWord)

    outputWord.x = 500

    outputWord.y = 500

Tips and warnings

  • Copy the code listings directly into the ActionScript editor to try the code listed above yourself. The code has been pretested and works.
  • When copying and pasting code from web pages, be sure you don't copy noncode items such as periods at ends of sentences. This will cause your program to generate errors that may be difficult to track down. Errors will often indicate that a specific line of code is the source of the error, when the error is actually in a line of code that follows that line.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.