How to Create a Text Field in AS3

Text fields, also known TextFields in AS3, are one of the fundamental objects that beginners need to know to create interactive Flash programs. In Flash animations, movies and video games, text boxes are used to display instructions, label buttons, and print messages like "you won." Although you can create text boxes without AS3 using the flash text field tool, you won't be able to display different messages in these text fields easily. Using AS3 lets you alter the text in text fields in response to different actions taken, which lets you add the interactivity most Flash projects need.

Things You'll Need

  • Adobe Flash Professional(CS3, CS4 or CS5 versions)
Show More

Instructions

    • 1

      Start the Flash program. Click "New" from the File menu on the main Flash menu bar. Click the file type "Flash File(ActionScript 3.0)" from the New Document dialog box that appears. Click the "OK" button to close this dialog box.

    • 2

      Select the "Actions" option from the Window menu on the main Flash menu bar to open the Actions editor. Position your mouse cursor on the first line of the Actions editor. Click your mouse button and type in the code below to declare a TextField named "myTextField" and an integer variable named "score" set to an initial value of 20.

      var myTextField:TextField = new TextField();

      var score:int = 20;

    • 3

      Type the code below starting at the next line in the Actions editor to set the width of the TextField to 200 pixels and the height of the TextField to 20 pixels.

      myTextField.width = 200;

      myTextField.height = 20;

    • 4

      Type the code below starting at the next line in the Actions editor to set the Flash stage coordinate position of the top left corner of the TextField to (200,200).

      myTextField.x = 200;

      myTextField.y = 200;

    • 5

      Type the code below starting at the next line in the Actions editor to fill the TextField with the color yellow and create a red border around the perimeter of the TextField.

      myTextField.background = true;

      myTextField.backgroundColor = 0xFFFF00;

      myTextField.border = true;

      myTextField.borderColor = 0xFF0000;

    • 6

      Type the code below starting at the next line in the Actions editor to instruct Flash to place the TextField on the stage with the message "Your score is 20" when the Flash movie plays.

      addChild(myTextField);

      myTextField.text = "Your score is " + score;

    • 7

      Review the code you typed in, as listed below, for syntax errors. Correct any errors as necessary. Copy and paste the code below into the Actions editor if you did not type in the code above. Use the code below to ensure that the TextField is displayed as intended and that a program error doesn't occur.

      var myTextField:TextField = new TextField();

      var score:int = 20;

      myTextField.width = 200;

      myTextField.height = 20;

      myTextField.x = 200;

      myTextField.y = 200;

      myTextField.background = true;

      myTextField.backgroundColor = 0xFFFF00;

      myTextField.border = true;

      myTextField.borderColor = 0xFF0000;

      addChild(myTextField);

      myTextField.text = "Your score is " + score;

    • 8

      Click the "TestMovie" option in the "Control" menu on the main Flash menu bar to play the movie. Observe that a yellow text field with a red border appears in the center of the stage with the text that was stored in the TextField, "Your score is 20."

Learnify Hub © www.0685.com All Rights Reserved