Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flash Developer Center /

Event handling in ActionScript 3

by H. Paul Robertson

H. Paul Robertson
  • Blog

Content

  • Clicking a button to play the current movie clip
  • Detecting typing in a text field
  • Clicking a button to navigate to a URL

Modified

23 November 2010

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScript events Flash Professional

Requirements

User level

Beginning

Required products

  • Flash Professional (Download trial)

Sample files

  • click_to_play_source.zip (36 KB)
  • detecting_typing_source.zip (12 KB)
  • click_to_navigate_source.zip (12 KB)

You can think of events as occurrences of any kind in your SWF file that are of interest to you as a programmer. For example, most SWF files support user interaction of some kind—whether it's something as simple as responding to a mouse click or something more complex, such as accepting and processing data entered into a form. Any such user interaction with your SWF file is considered an event. Events can also occur without any direct user interaction, such as when data has finished loading from a server or when an attached camera has become active.

In ActionScript 3.0, each event is represented by an event object, which is an instance of the Event class or one of its subclasses. An event object stores information about a specific event and contains methods that facilitate manipulation of the event object. For example, when Flash Player detects a mouse click, it creates an event object (an instance of the MouseEvent class) to represent that particular mouse click event.

After creating an event object, Flash Player dispatches it, which means that the event object is passed to the object that is the target of the event. An object that serves as the destination for a dispatched event object is called an event target. For example, when an attached camera becomes active, Flash Player dispatches an event object directly to the event target, which in this case is the object that represents the camera. If the event target is a display object on the screen, however, the event object is passed from the Stage up through the display object containers that contain the event target (the display list), until it reaches the event target. In some cases, the event object then "bubbles" back up the display list hierarchy along the same route. This traversal of the display list hierarchy is called the event flow. The event flow is not described in this QuickStart article; for more information see the list of resources at the end of the article.

Your code can listen for events using event listeners. Event listeners are the functions or methods that you write to respond to specific events. To ensure that your program responds to events, you must add event listeners either to the event target or to any display list object that is part of an event object's event flow.

Any time you write event listener code, it follows this basic structure (the elements in bold are placeholders you'd fill in for your specific case; more concrete examples follow):

function eventHandlerFunction(eventObject:EventType):void { // Actions performed in response to the event go here. } eventTarget.addEventListener(EventType.EVENT_NAME, eventHandlerFunction);

This code does two things:

  1. It defines a function, which is the way to specify the actions that will be performed in response to the event.
  2. It calls the addEventListener() method of the source object, in essence "subscribing" the function to the specified event so that when the event happens, the function's actions are carried out.

When the event actually happens, the event target checks its list of all the functions and methods that are registered as event listeners. It then calls each one in turn, passing the event object as a parameter.

You need to alter four elements in the preceding code to create your own event listener.

  • Change the name of the function to the name you want to use (this must be changed in two places, where the code says eventHandlerFunction)
  • Substitute the appropriate class name of the event object that is dispatched by the event you want to listen for (EventType in the code)
  • Substitute the appropriate constant for the specific event (EVENT_NAME in the listing)
  • Change the addEventListener() method call so that it's called on the object that will dispatch the event (replacing eventTarget in the preceding code)
  • Optionally, you can also change the name of the variable used as the function's parameter (eventObject in the code listing)

The following sections show examples of event handling code to give you an idea of some of the common event elements and possible variations available when you write event-handling code.

Clicking a button to play the current movie clip

The following example demonstrates creating a clickable button that is used to play a movie clip (for example, the main timeline in Flash). In the code listing, playButton is the instance name of the button, and this is a special name meaning "the current object" (which in this case is assumed to be a movie clip).

Example

import flash.events.MouseEvent; this.stop(); function playMovie(event:MouseEvent):void { this.play(); } playButton.addEventListener(MouseEvent.CLICK, playMovie);

Result

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

To get the source files for this example, download click_to_play_source.zip from the top of the page, or right-click the SWF above and select Download Source from the context menu. The Flash Professional CS5 version of the FLA file is in the ZIP file as click_to_play_CS5.fla.

Detecting typing in a text field

The following example demonstrates using the textInput event of a text field to detect when the user enters text in the text field. In this example it simply writes the user's keystroke back to the screen. However, the same event could be used for more complex tasks. For example, the code could be expanded to automatically format text such as a phone number, or to check that the text matches certain allowable characters or formatting. In the following code listing, entryText is an input text field, and outputText is a dynamic text field.

Example

import flash.events.TextEvent; function updateOutput(event:TextEvent):void { var pressedKey:String = event.text; outputText.text = "You typed: " + pressedKey; } entryText.addEventListener(TextEvent.TEXT_INPUT, updateOutput);

Result

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

To get the source files for this example, download detecting_typing_source.zip from the top of the page, or right-click the SWF above and select Download Source from the context menu. The Flash Professional CS5 version of the FLA file is in the ZIP file as detecting_typing_CS5.fla.

Clicking a button to navigate to a URL

This example shows how to create a button that, when clicked, navigates the browser to the specified URL. The button essentially behaves like a hyperlink in a web page. In this code listing, linkButton is the instance name of the button.

Example

import flash.events.MouseEvent; function gotoAdobeSite(event:MouseEvent):void { var adobeURL:URLRequest = new URLRequest("http://www.adobe.com/"); navigateToURL(adobeURL); } linkButton.addEventListener(MouseEvent.CLICK, gotoAdobeSite);

Result

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

To get the source files for this example, download click_to_navigate_source.zip from the top of the page, or right-click the SWF above and select Download Source from the context menu. The Flash Professional CS5 version of the FLA file is in the ZIP file as click_to_navigate_CS5.fla.


For more information

This article was adapted from the sections "Introduction to handling events" and "Event-handling examples" in Programming ActionScript 3.0. For more information about handling events in ActionScript 3.0, see the following resources:

  • Getting Started with ActionScript > Events in Learning ActionScript 3.0
  • Handling events in the ActionScript 3.0 Developer's Guide
  • Flex Quick Start: Handling events" in the Flex Developer Center

Related Flash Quick Starts

  • Embedding fonts
  • Loading images and Library assets in Flash with ActionScript 3
  • Loading and saving local files with the FileReference class
  • Display list programming
  • Creating a simple ActionScript 3 class
  • Working with symbols and the Document class
  • Programming with arrays
  • Programming with the Vector class

More Like This

  • Display list programming in ActionScript 3
  • Using ActionScript 3 drawing commands
  • Programming with the Vector class
  • Creating, populating, and resizing the DataGrid component
  • Customizing and sorting the DataGrid component
  • Filtering and formatting data in the DataGrid component
  • Getting started with Flash CS4 user interface components
  • Using the Label component
  • Loading images and Library assets with ActionScript 3
  • Displaying images with the TileList component

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement