Macromedia Flash Professional 8 UNLEASHED [Electronic resources]

David Vogeleer, Eddie Wilson, Lou Barber

نسخه متنی -صفحه : 318/ 147
نمايش فراداده

Events of the TextField Object

Flash uses text fields as a means of gathering and displaying information. And because they are objects, they also have events associated with them.

Table 14.5 lists those events.

Table 14.5. TextField Events

Event Handler Methods

Action Description

onChanged

The event invoked when the text in a text field changes

onScroller

The event triggered when the scroll property of a text field changes

onSetFocus

The event called when a text field receives keyboard focus (for example, if a user uses the Tab key to gain focus on the text field)

onKillFocus

The event called when a text field loses keyboard focus

TextField objects support listeners and callbacks.

Here are two examples doing the same thing using both means of capturing an event.

First, the callback way:

1.

Create a new Flash document.

2.

Draw an input text field on the stage.

3.

Give the text field an instance name of

input_txt and make sure the border option is selected (so that you can see it on the stage).

4.

Create a new layer and name it

actions .

5.

In the actions layer, open the Actions panel and place these actions in it:

//create the callback
input_txt.onChanged = function(){
trace("changes have been made");
}

The preceding code creates a callback such that when a user makes changes to the content in the input_txt text field, a message will be sent to the Output panel.

That example used a callback to capture the onChanged event. This example uses a listener:

1.

Create a new Flash document.

2.

Draw an input text field on the stage.

3.

Give the text field an instance name of

input_txt and make sure the border option is selected (so that you can see it on the stage).

4.

Create a new layer and name it

actions .

5.

In the actions layer, open the Actions panel and place these actions in it:

//create the object to listen for the event
var changeListen:Object = new Object();
//create the event method
changeListen.onChanged = function(){
trace("changes have been made");
}
//add the listener to the TextField 
input_txt.addListener(changeListen);

The preceding code creates the listener object. Then it creates the event-handling method, which, when triggered, will send a message to the Output panel. Finally, we add the listener object to the text field instance.

Both of these examples accomplish the same tasks, but in different ways. You choose to use the one you feel most comfortable with.

And when dealing with events you do not have to just use certain events with certain objects. In some circumstances, you can use events with objects that they were not meant for.