13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Using the TextField class<br />

var myTextField:TextField = new TextField();<br />

myTextField.text = "Please select the TEXT IN ALL CAPS to see the index values for the first<br />

and last letters.";<br />

myTextField.autoSize = TextFieldAutoSize.LEFT;<br />

addChild(myTextField);<br />

addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.MOUSE_UP, selectText);<br />

function selectText(ev<strong>en</strong>t:MouseEv<strong>en</strong>t):void<br />

{<br />

trace("First letter index position: " + myTextField.selectionBeginIndex);<br />

trace("Last letter index position: " + myTextField.selectionEndIndex);<br />

}<br />

You can apply a collection of TextFormat object properties to the selection to change the text appearance. For more<br />

information about applying a collection of TextFormat properties to selected text, see “Formatting ranges of text<br />

within a text field” on page 383.<br />

Capturing text input<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

By default, the type property of a text field is set to dynamic. If you set the type property to input using the<br />

TextFieldType class, you can collect user input and save the value for use in other parts of your application. Input text<br />

fields are useful for forms and any application that wants the user to define a text value for use elsewhere in the<br />

program.<br />

For example, the following code creates an input text field called myTextBox. As the user <strong>en</strong>ters text in the field, the<br />

textInput ev<strong>en</strong>t is triggered. An ev<strong>en</strong>t handler called textInputCapture captures the string of text <strong>en</strong>tered and<br />

assigns it a variable. Flash Player or AIR displays the new text in another text field, called myOutputBox.<br />

package<br />

{<br />

import flash.display.Sprite;<br />

import flash.display.Stage;<br />

import flash.text.*;<br />

import flash.ev<strong>en</strong>ts.*;<br />

public class CaptureUserInput ext<strong>en</strong>ds Sprite<br />

{<br />

private var myTextBox:TextField = new TextField();<br />

private var myOutputBox:TextField = new TextField();<br />

private var myText:String = "Type your text here.";<br />

public function CaptureUserInput()<br />

{<br />

captureText();<br />

}<br />

public function captureText():void<br />

{<br />

myTextBox.type = TextFieldType.INPUT;<br />

myTextBox.background = true;<br />

addChild(myTextBox);<br />

Last updated 6/6/2012<br />

378

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!