How to Code a Line Break in AS3

Coding a line break in Flash's ActionScript programming language (specifically the AS3 version of ActionScript) lets you display text in a Flash animation or Flash movie so that text will fit within the width you have allocated for your text. Because ActionScript doesn't automatically insert a line break when you press the enter key when coding, you must place the equivalent code for a line break in any text where you want ActionScript to display on different lines. You can do this by inserting a two-character code in your text.

Things You'll Need

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

Instructions

    • 1

      Click "New" from the "File" menu on the main Flash menu bar to start the Flash program. Click the file type "Flash File(ActionScript 3.0)" from the New Document dialog box that appears to select the AS3 version of ActionScript. 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. Type the code below to instruct Flash to declare a TextField variable that will hold text.

      var nextLine:TextField = new TextField();

    • 3

      Type the code below in the next line of the Actions editor to set the width of the TextField to 200 pixels.

      nextLine.width = 200;

    • 4

      Type the code below in the next line of the Actions editor to instruct Flash to display the text "Print this on the first line" on one line and to display "Print this on the second line" on the next line. Observe that the character code ""\n" is used to instruct Flash to display the text that follows it on a new line.

      nextLine.text = "Print this on the first line.\nPrint this on the second line";

    • 5

      Type the code below in the next line of the Actions editor to add the TextField to the display when you play your Flash movie.

      addChild(nextLine)

    • 6

      Review the code you typed in as listed below for syntax errors and correct as necessary. Copy and paste the error-free code below if you have not typed in the code in the previous steps and to assure that your Flash program will play without generating errors.

      var nextLine:TextField = new TextField();

      nextLine.width = 200;

      nextLine.text = "Print this on the first line.\nPrint this on the second line";

      addChild(nextLine);

Learnify Hub © www.0685.com All Rights Reserved