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 / ActionScript Technology Center /

Understanding the changes in the display API in ActionScript 3.0

by Bill Lee

Bill Lee
  • FlashSeer

Created

5 March 2007

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScript ActionScript 2  Flash Builder Flash Professional migration OOP

Requirements

Prerequisite knowledge

Basic understanding of ActionScript.

User level

Beginning

Required products

  • Flash Builder (Download trial)
  • Flash Professional (Download trial)

Sample files

  • display_api_example.zip (10 KB)

There are many differences between ActionScript 2.0 and ActionScript 3.0. This article focuses on the changes in the display API. In this article, I provide two very simple Adobe Flash files, one written in ActionScript 2.0 and the other written in ActionScript 3.0. Though these applications are very simple, you can use them to learn the basics of how the display API works in ActionScript 3.0. The article covers only the basics of the display API. If you need more information about it, please refer to the sources mentioned at the end of the article.

The first section of this article shows you some simple code written in ActionScript 2.0 that draws a red rectangle and a text field. When you click the rectangle, the text field changes its text. In the second section, I rewrite this example in ActionScript 3.0—and, before I show you the code, I explain a little bit about the new concepts that ActionScript 3.0 uses. In this process, you will learn some basic ideas of how the display system works in ActionScript 3.0.

A very simple example written in ActionScript 2.0

Take a look at the example written in ActionScript 2.0. The following script draws a red rectangle on the Stage:

var shape_mc:MovieClip=_root.createEmptyMovieClip("shape",_root.getNextHighestDepth()); shape_mc.lineStyle(1, 0x000000); shape_mc.beginFill(0xff0000); shape_mc.moveTo(0, 0); shape_mc.lineTo(50, 0); shape_mc.lineTo(50, 50); shape_mc.lineTo(0, 50); shape_mc.lineTo(0, 0); shape_mc.endFill();

This code creates an empty movie clip by using the createEmptyMovieClip() method of the top level _root object. Then I use the drawing methods of the newly created movie clip to draw a red rectangle.

To have this movie clip respond to a mouse click, I wrote an event handler:

shape_mc.onPress=function(){ text_txt.text="You pressed the rectangle"; }

The following code creates a text field:

var text_txt = _root.createTextField("text", _root.getNextHighestDepth(), 100, 0, 150, 20); text_txt.text = "Click the rectangle.";

It creates a text field using the createTextField() method of the _root object and assigns a value to the text property of the text field.

You can view the Flash content by pressing Control + Enter in Flash. The SWF file is pretty simple: A user clicks the rectangle and the value of text field changes.

Rewriting the example in ActionScript 3.0

Now I will write the same example using ActionScript 3.0. Before I actually write any code, I need to explain a few things.

Figure 1 shows the display list of an ActionScript 3.0 application. The display list is a tree structure that contains all display objects that have been added to the Stage. As you can see, there is no "_root" movie clip in ActionScript 3.0. The top-level display object in ActionScript 3.0 is the Stage. Other display objects descend from that top-level object. The default display object is the MovieClip object, which represents the SWF file itself. It is the parent of all other display objects. The two display objects that the code creates are added as children of the default display object.

Display list in ActionScript 3.0 for this example
Figure 1. Display list in ActionScript 3.0 for this example

In the ActionScript 2.0 example, the _root object itself is a movie clip. Actually, in ActionScript 2.0, there is not much choice when you need to create a displayable object. It has to be a movie clip or a text field.

By contrast, ActionScript 3.0 includes many display object classes, such as Shape, Sprite, MovieClip, and so forth—these are included for a reason. In ActionScript 2.0, you can use a MovieClip object to draw shapes and lines, interact with a movie clip, and load a SWF file into a movie clip. However, sometimes you only need to draw an object, without using all of the other functionality. In this case, creating a fully functional movie clip wastes memory and CPU time. In ActionScript 3.0, you can use a Shape object. The Shape class contains the drawing API but it doesn't support interaction or loading another movie clip, so it saves resources compared to using a fully functional MovieClip object.

Let me show you the ActionScript 3.0 code. First, it draws the red rectangle:

var mySprite:Sprite = new Sprite(); mySprite.graphics.beginFill(0xff0000); mySprite.graphics.lineStyle(1, 0x000000); mySprite.graphics.drawRect(0, 0, 50, 50); mySprite.graphics.endFill();

The Sprite class defines a type of display object in ActionScript 3.0. It supports drawing and interaction. I use it because the rectangle needs to respond to a mouse click by the user. If you don't need interaction, just use the Shape class.

Now the code declares a variable named mySprite and uses the new keyword to create a new instance of a Sprite object. Note that the code does not draw the rectangle. Flash doesn't draw the rectangle through a method of the Sprite object; rather, it draws the rectangle using a method of the graphics property of the Sprite object. Each of the classes that support drawing (Shape, Sprite, and MovieClip) has a property named graphics. This property is an instance of the Graphics class. It contains all of the drawing methods.

Then the code includes add the event hander for the click event (which is defined by the MouseEvent.CLICK constant). Adding an event listener is not the focus of this article, so I will not go into further details here. Just look at the code; it is similar to the way you add event listeners to an object in ActionScript 2.0:

function clickHandler(event:Event):void { txt.text="You clicked the rectangle."; } mySprite.addEventListener(MouseEvent.CLICK,clickHandler);

After that, the code adds the newly created mySprite object to the top-level container (the default display object in Figure 1):

addChild(mySprite);

Next, the code instantiates a text field:

var txt:TextField = new TextField(); txt.x = 100; txt.y = 0; txt.width = 150; txt.height = 20; txt.text = "Click the rectangle.";

It's easy to understand the code that creates the text field. It is similar to the code that creates the Sprite instance.

Finally, the code uses the addChild() method to add this text field to the top-level container:

addChild(txt);

Run this example; you will find that it acts exactly as the ActionScript 2.0 version does.

Where to go from here

In the two examples in this tutorial, I demonstrate how to use ActionScript 3.0 to display content. It is a simple example that illustrates the basics of creating display objects in ActionScript 3.0.

This article discusses only a small part of the API. If you want to learn more about ActionScript 3.0, refer to the ActionScript language migration page, which describes the language differences between ActionScript 2.0 and 3.0.

Attributions

I researched this topic using the LiveDocs ActionScript Language Reference.

More Like This

  • Top five misperceptions about ActionScript 3
  • ActionScript 3.0 Quick Reference Guide excerpts: Copy motion, display list, events, and migration
  • Handling audio file cue points in ActionScript 2 and ActionScript 3
  • Top five benefits of ActionScript
  • Tips for learning ActionScript 3.0
  • Introduction to event handling in ActionScript 3.0
  • Developers and designers talk about migrating to ActionScript 3
  • ActionScript 3.0 overview
  • ActionScript 3.0 from the ground up
  • Essential ActionScript 3 excerpt: Display and interactivity

Tutorials & Samples

Tutorials

  • Flash iOS Apps Cookbook excerpt: ActionScript optimization
  • Introduction to Robotlegs – Part 3
  • Introduction to Robotlegs – Part 2
  • Introduction to Robotlegs – Part 1

Samples

  • ActionScript 3 samples for Flash Professional CS5

ActionScript Blog

More
06/09/2012 New Flash Tools: Flash Optimizer 2.3
06/09/2012 Flash Optimizer 2.3
05/27/2012 How to add an image to android gallery (CameraRoll) using adobe air
04/22/2012 ActionScript 3.0 BitmapData, Clone, and ColorTransform

ActionScript Cookbook

More
03/20/2012 PDF Portfolio Navigational TOC
02/29/2012 Embed Stage3D content inside Flex application components
02/13/2012 Randomize an array
02/09/2012 Using Camera with a MediaContainer instead of VideoDisplay

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