How to Display Strings Using AS3

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 addChild command.

Things You'll Need

  • Adobe Flash Professional
Show More

Instructions

    • 1

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

    • 2

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

    • 3

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

    • 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

      Write the fifth line of code to place the TextField, outputWord, on the stage (the screen): addChild(outputWord)

    • 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

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

      var sign: String

      sign ="OPEN"

      var outputWord:TextField = new TextField()

      outputWord.text = sign

      addChild(outputWord)

      outputWord.x = 250

      outputWord.y = 250

    • 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 select the "Test Movie" option from the "Control" menu to view the movie.

    • 9

      Change the code so that the 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 outputWord:TextField = new TextField()

      outputWord.text = sign

      addChild(outputWord)

      outputWord.x = 500

      outputWord.y = 500

Learnify Hub © www.0685.com All Rights Reserved