ActionScript migration cookbook

laborermaizeΛογισμικό & κατασκευή λογ/κού

4 Ιουλ 2012 (πριν από 5 χρόνια και 2 μήνες)

368 εμφανίσεις

migration cookbook
ActionScript
®
3
For Adobe
®
Flash
®
CS4 Professional
2
ActionScript 3 migration cookbook
3
adobe.com/go/as3migration
©2009 Adobe Systems Incorporated. All rights reserved. Adobe, the Adobe logo, ActionScript, the Adobe Flash
logo, Flash, Pixel Bender, are either registered trademarks or trademarks of Adobe Systems Incorporated in the
United States and/or other countries. Printed in the USA. 95012173 03/09
This work is released under a Creative Commons Attribution-Noncommercial
3.0 Unported License.
creativecommons.org/licenses/by-nc/3.0
Table of Contents
Introduction
Top 5 misperceptions about ActionScript 3
Top 5 benefits of ActionScript 3
Migration cookbook
Notes
Create an untyped variable
Create a typed variable
Create an untyped function
Create a typed function
Create an if/else statement
Create and loop through an array
Create a random number within a range
Access root/main timeline of your content
Handling button interactions
Open a URL
Listen for key presses
Dynamically attach a MovieClip from the library
Dynamically create and draw on a MovieClip
Dynamically set the color of a MovieClip
Dynamically swap MovieClip depths
Dynamically create and loop through MovieClip instances
Play an embedded sound
Dynamically load and play a sound
Dynamically load and display an image
Load and read XML
11
12
13
14
15
16
17
18
19
20
21
22
24
26
27
28
32
34
35
36
4
6
8
10
38
About this guide
This guide provides a quick introduction to migrating to ActionScript 3 from ActionScript 2.
It is targeted at designers and developers who have some experience scripting content
within Adobe Flash Professional. It does not require an understanding of
object-oriented programming.
This guide is intended as a quick guide and reference that complements the more
extensive migration resources and documentation available on the
ActionScript 3 Technology Center:

adobe.com/go/as3migration
How to use this guide
This guide can be used as both a high-level introduction to ActionScript 3, and a reference
for accomplishing frequent tasks when creating content in Flash Professional. The guide is
broken down into a number of sections:
Introduction: Provides a high-level introduction to ActionScript 3
Top misperceptions: Dispels some of the top misperceptions around ActionScript 3
Top benefits: Lists some of the top benefits of ActionScript 3
Migration cookbook: Shows how to do common tasks in ActionScript 3
4
ActionScript 3 migration cookbook
What is ActionScript 3?
ActionScript 3 migration gotchas
5
adobe.com/go/as3migration
ActionScript 3 is the most current version of the language used for scripting and
programming interactive content for the Adobe Flash Platform. The Flash Platform
is a complete system of integrated tools, frameworks, clients and servers that can be
used to create and deploy Web applications, content and video that run consistently
across operating systems and devices, leveraging the ubiquity of Adobe Flash Player
and Adobe AIR®.
ActionScript 3 was created in order to provide significant performance improvements
over ActionScript 2, and to make developing applications easier and more maintainable.
However, even though ActionScript 3 was largely targeted at rich Internet application
development, its strengths and benefits can also be leveraged when creating more
expressive and creative content in Flash Professional, such as animations or
motion graphics.
In general, ActionScript 3 can be broken down into two main parts: the core language,
and the programming APIs. The semantics of the core language are not much different
from ActionScript 2 and should not present much of a learning curve. However, the Flash
Player APIs have undergone some significant changes, which means that some common
tasks are done differently in ActionScript 3. This reference focuses on some of the new
ActionScript 3 APIs which are used often, but are different enough from their
ActionScript 2 counterparts to potentially cause confusion.
While learning the new APIs can be frustrating at times, the improved consistency
between APIs in ActionScript 3 means that knowledge learned while using one particular
API can be applied to learning other APIs. It is this improved consistency between APIs
which can significantly reduce the migration and learning curve for ActionScript 3.
Below is a list of changes that may trip you up when you first begin working with
ActionScript 3. You can find examples of all of the items in the cookbook entries.
Use of underscore in MovieClip property names
In ActionScript 3, the use of the underscore (“_”) at the beginning of property names for the
MovieClip object have been removed in order to provide greater consistency with the rest
of the ActionScript APIs. For example, the ActionScript 2
_x
,
_y
,
_alpha
and
_rotation
properties have been changed to
x
,
y
,
alpha
,
and
rotation

in ActionScript 3.
Note that because the MovieClip class is dynamic, if you use the old underscore
property names in ActionScript 3, you will not get an error and the property will
not be set correctly.
Void
has changed to
void
In ActionScript 2,
Void
is used to denote when a function does not return anything.
In ActionScript 3, the term is now
void
(all lowercase).
Alpha value ranges
In ActionScript 2, alpha/transparency values are expressed in a range between 0 and 100.
In ActionScript 3, alpha values are now expressed in a range between 0.0 and 1.0.
Events
ActionScript 3 includes a built-in event model which is used throughout the language.
The advantages of the new API include a standard way to handle events, the ability for
multiple items to listen to the same event, and correct scoping of event handlers.
The syntax for specifying event handlers has changed and you can no longer place
event handles directly on symbols within Flash CS4 Professional.
Common compiler errors when migrating to ActionScript 3
Below are a some compile-time errors that are common when migrating content
to ActionScript 3.
1067: Implicit coercion of a value of type Number to an unrelated type String.
You are trying to store a value of one type into a variable that is set to store another type.

For example:
var s:String;
var n:Number = 5;
n = s;
1136: Incorrect number of arguments. Expected 1.
You are calling a function without passing in the correct number of arguments.
1152: A conflict exists with inherited definition flash.display:DisplayObject.transform
in namespace public.
You have a variable name that conflicts with a built-in property name
(most likely contained within the DisplayObject class).
6
ActionScript 3 migration cookbook
7
adobe.com/go/as3migration
1. ActionScript 3 is difficult to learn
ActionScript 3 is no more difficult to learn than any other programming or scripting
language. If you are familiar with ActionScript 2, the language semantics are pretty
much the same, although you will need to learn new ways of doing some common tasks.
Because the ActionScript 3 APIs are more consistent, learning one new concept and
API applies to multiple APIs, making it easier to use newly found knowledge to learn
new features and functionality.
2. ActionScript 3 is only for object-oriented programmers
ActionScript 3 can be used for both class-based, object-oriented programming, as well
as timeline-based scripting. You can use it in whichever way is the most comfortable
for you or makes the most sense for your project.
3. Targeting ActionScript 3 reduces the Flash Player base that you can target
As of December 2008, content targeted for Flash Player 9, the first version that supported
ActionScript 3, can be viewed by 98.6% of computers on the Internet.
4. You can’t write code on the timeline with ActionScript 3
You can place code on the timeline just as you can with ActionScript 1 and 2.
5. ActionScript 2 development is faster than ActionScript 3
While some tasks can require more code in ActionScript 3 than in ActionScript 2, overall
development and maintenance time should be the same or less than in ActionScript 2.0
due to improved debugging and better compile-time error catching. Basically, in some
cases there may be more code, but it will be much easier to find errors.
about ActionScript 3
Top
misperceptions
8
ActionScript 3 migration cookbook
9
adobe.com/go/as3migration
1. Achieve greater performance
ActionScript 3 was written from the ground up with performance in mind. Depending
on the content, you can see a significant increase in performance. This means that
your existing content may run smoother, and your new content can do more, while
using the same amount of CPU resources.
2. Leverage new Flash Player APIs
Adobe Flash Player 9 and 10 includes a ton of new features which can only be used
through ActionScript 3. These include much easier XML APIs via E4X, more advanced
display list manipulation, or doing advanced image manipulation with Pixel Bender™ filters.
As a general rule, new ActionScript-based features added in the future will only be
available via ActionScript 3.
3. Leverage community libraries and APIs
Almost all of the major new libraries released by the community are built with
ActionScript 3, and include everything from the Papervision3D full 3D engine
library to Grant Skinner’s Gtween animation library.
4. Troubleshoot code more easily
The ActionScript 3 compiler provides options for much stricter error checking, which
means it is more likely you are going to find bugs and errors before you even begin to run
your content. When you do find errors, you can take advantage of some of the new and more
advanced debugging features to track them down (and of course, you can still use
trace()
).
5. Develop content for multiple platforms
ActionScript 3 is the standard language used across the Adobe Flash Platform. Moving
forward, it is the language that Adobe will focus on supporting in existing and new players
(like Adobe AIR), servers, and products. Flash Player 10 supports ActionScript 3, and its use
is required for developing for Adobe Flex and Adobe AIR content. In addition, Adobe is
working on updating its mobile runtimes to ActionScript 3.
In the future, you can expect that new Flash Platform products, runtimes, and services from
Adobe will use ActionScript 3.
Top
benefits
of ActionScript 3
cookbook: Create an untyped variable
Task: You need to create a variable to store a value.
Solution: Use the
var
keyword to declare a variable.
Note: Just as in ActionScript 2, you use the
var
keyword only the first time you declare the variable.
ActionScript 3
var s = "I am a String";
var i = 5;
ActionScript 2
var s = "I am a String";
var i = 5;
10
ActionScript 3 migration cookbook
11
adobe.com/go/as3migration
migration
cookbook
ActionScript 2.0 to ActionScript 3.0
cookbook: Create a typed variable
Task: You need to create a variable to store a specific type of value.
Solution: Use the
var
keyword and specify the variable type.
Note: Once you type the variable, you can only store data of that type in the variable.
For example, the following would throw an error:
var s:String = "I am a String";
var i:Number = 5;
s = i;
because you are trying to store a
Number
in a variable which you specified was a
String
.
The advantage of typing variables is that, in general, it makes your code a little easier to read, the player can
run your code faster, and the compiler can find some errors when you compile your content.
All of the examples in this cookbook use typed variables.
ActionScript 3
var s:String = "I am a String";
var i:Number = 5;
ActionScript 2
var s:String = "I am a String";
var i:Number = 5;
cookbook: Create an untyped function
Task: You need to create a function that takes an argument and then call it.
Solution: Use the
function
keyword to create a function that takes a variable.
Note: Unlike in ActionScript 2, in ActionScript 3 if you attempt to call the function without any arguments,
you will get an error.
For example, he following would throw an error:
function foo(bar)
{
trace(bar);
}
foo();
ActionScript 2
function foo(bar)
{
trace(bar);
}
foo("hello");
ActionScript 3
function foo(bar)
{
trace(bar);
}
foo("hello");
12
ActionScript 3 migration cookbook
13
adobe.com/go/as3migration
cookbook: Create a typed function
Task: You need to create a function that specifies the type of its arguments, as well
as its return type.
Solution: Use the
function
keyword and type identifiers to create a function that
takes and returns a typed value.
Note: We specify the return type as
void
, which means that the function does not return a value.
Also note that in ActionScript 3 the usage is
void
, and not
Void
as in ActionScript 2.
If you try to call the function in ActionScript 3 and pass it a
Number
instead of a
String
, you
will get an error.
ActionScript 2
function foo(bar:String):Void
{
trace(bar);
}
foo("
h
ello");
cookbook: Create an if/else statement
Task: You need to branch your code depending on a value in your content.
Solution: Use an if/else statement to determine what code to run.
Note: You can use the

if

statement without the
else if
, and
else
parts.
ActionScript 3
var value:Number = 5;
if(value > 5)
{
trace("value is greater than 5");
}
else if(value < 5)
{
trace("
v
alue is less than 5");
}
else
{
trace("value equals 5");
}
ActionScript 2
var value:Number = 5;
if(value > 5)
{
trace("
v
alue is greater than 5");
}
else if(value < 5)
{
trace("
v
alue is less than 5");
}
else
{
trace("
v
alue equals 5");
}
ActionScript 3
function foo(bar:String):void
{
trace(bar);
}
foo("
h
ello");
14
ActionScript 3 migration cookbook
15
adobe.com/go/as3migration
cookbook: Create and loop through an Array
Task: You need to create an Array and loop through its values.
Solution: Use a
for
loop to loop through the Array.
Note: You can also use the
Array
constructor and
push
method to populate an Array like so:
var a:Array = new Array();
a.push("a");
a.push("b");
a.push("c");
a.push("d");
ActionScript 3
var a:Array = ["a", "b", "c", "d"];
var len:Number = a.length;
for(var i:Number = 0; i < len; i++)
{
trace(a[i]);
}
ActionScript 2
var a:Array = ["a", "b", "c", "d"];
var len:Number = a.length;
for(var i:Number = 0; i < len; i++)
{
trace(a[i]);
}
cookbook: Create a random number within a range
Task: You need to create a random absolute number between 0 and 10.
Solution: Use new
Math
APIs to generate a random number.
Note: The
Math.random
and
Math.round
functions are available in ActionScript 2, but are included
here for completeness.
ActionScript 3
var rand:Number = Math.random();
var max:Number = 10;
var randInRange:Number = Math.round(rand * max);
trace(randInRange);
ActionScript 2
var max:Number = 10;
var randInRange:Number = random(10 + 1);
trace(randInRange);
16
ActionScript 3 migration cookbook
17
adobe.com/go/as3migration
cookbook: Access root/main timeline of your content
Task: You need to access the root / main timeline of your content.
Solution: Use the
root
property to access the root timeline of your content.
Note: In ActionScript 2

_root
always refers to the main timeline of the main SWF. In ActionScript 3, the
root

property refers to the main timeline of the SWF in which the content originates. In most cases, these will
be the same in either version of ActionScript. The main exception is where the property is being called from
within a SWF that has been loaded into another SWF. In ActionScript 2

_root
will refer to the main timeline
of the main SWF (that loaded the other SWF). In ActionScript 3 the
root
property refers to the main timeline
of the loaded SWF (and not the timeline of the SWF which loaded the other SWF).
The
root
property is only available in
DisplayObject
instances (such as
MovieClip
,
Sprite
and
Button
).
ActionScript 3
//function on main timeline
function foo():void
{
trace("foo");
}
//called from anywhere in SWF
MovieClip(root).foo();
ActionScript 2
//function on main timeline
function foo():Void
{
trace("foo");
}
//called from anywhere in content
_root.foo();
cookbook: Handling button interactions
Task: You need to determine when the user interacts with a button.
Solution: Listen for events broadcast by the button.
Note: Both examples assume there is a button symbol instance named "my_button" on the same timeline
as the code.
ActionScript 3
function onButtonClick(event:MouseEvent):void
{
trace("button was clicked");
}
function onMouseOver(event:MouseEvent):void
{
trace("button was moused over");
}
my_button.addEventListener(MouseEvent.CLICK, onButtonClick);
my_button.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
ActionScript 2
function onButtonRelease():Void
{
trace("button was clicked");
}
function onButtonRollOver():Void
{
trace("button was moused over");
}
my_button.onRelease = onButtonRelease;
my_button.onRollOver = onButtonRollOver;
18
ActionScript 3 migration cookbook
19
adobe.com/go/as3migration
cookbook: Open a URL
Task: You need to open a URL in a new browser window.
Solution: Use
navigateToURL
to open URLs in ActionScript 3.
ActionScript 3
var url:URLRequest = new URLRequest("http://www.adobe.com");
navigateToURL(url, "_blank");
ActionScript 2
getURL("http://www.adobe.com", "_blank");
cookbook: Listen for key presses
Task: You need to detect when the user presses a key.
Solution: Listen for the
KeyboardEvent.KEY_UP
event.
ActionScript 3
function onKeyDownHandler(event:KeyboardEvent):void
{
var code:uint = event.keyCode;
var char:String = String.fromCharCode(code);
trace("Key Down : code : " + code + " char : " + char);
}
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyDownHandler);
ActionScript 2
function onKeyDown():Void
{
var code:Number = Key.getCode();
var char:String = String.fromCharCode(code);
trace("Key Down : code : " + code + " char : " + char);
}

Key.addListener(this);
20
ActionScript 3 migration cookbook
21
adobe.com/go/as3migration
cookbook: Dynamically attach a MovieClip from the library
Task: You need to dynamically attach a
MovieClip
from the library.
Solution: Use the
MovieClip
constructor to attach a symbol from the library.
Note: In ActionScript 2 the linkage ID , “my_clip” is set in the library for the
MovieClip
, whereas in
ActionScript 3, the class name , “my_clip” is set.
ActionScript 2
attachMovie("my_clip", "clip", 10);
clip._x = 100;
clip._y = 100;
clip._alpha = 50;
ActionScript 3
var clip:MovieClip = new my_clip();
clip.x = 100;
clip.y = 100;
clip.alpha = .5;
addChild(clip);
Setting the class name for a MovieClip symbol
for use in ActionScript 3
22
ActionScript 3 migration cookbook
23
adobe.com/go/as3migration
cookbook: Dynamically create and draw on a MovieClip
Task: You need to dynamically create a
MovieClip
, place it on the Stage and draw on it.
Solution: Use the
MovieClip
constructor to create the
MovieClip
and then draw into its
graphics
property.
Note: In ActionScript 2 drawing is done directly on the

MovieClip
, whereas in ActionScript 3, it is done on
the
graphics
property of the
MovieClip
.
ActionScript 2
function drawCircle(target_mc:MovieClip, radius:Number, fillColor:Number, fillAlpha:Number)
{
var x:Number = radius;
var y:Number = radius;
with (target_mc)
{
beginFill(fillColor, fillAlpha);
moveTo(x + radius, y);
curveTo(radius + x, Math.tan(Math.PI / 8) * radius + y, Math.sin(Math.PI / 4) *



radius + x, Math.sin(Math.PI / 4) * radius + y);
curveTo(Math.tan(Math.PI / 8) * radius + x, radius + y, x, radius + y);
curveTo(-Math.tan(Math.PI / 8) * radius + x, radius+ y, -Math.sin(Math.PI / 4) *


radius + x, Math.sin(Math.PI / 4) * radius + y);
curveTo(-radius + x, Math.tan(Math.PI / 8) * radius + y, -radius + x, y);
curveTo(-radius + x, -Math.tan(Math.PI / 8) * radius + y, -Math.sin(Math.PI / 4) *




radius + x, -Math.sin(Math.PI / 4) * radius + y);
curveTo(-Math.tan(Math.PI / 8) * radius + x, -radius + y, x, -radius + y);
curveTo(Math.tan(Math.PI / 8) * radius + x, -radius + y, Math.sin(Math.PI / 4) *



radius + x, -Math.sin(Math.PI / 4) * radius + y);
curveTo(radius + x, -Math.tan(Math.PI / 8) * radius + y, radius + x, y);
endFill();
}
}
createEmptyMovieClip("circle_mc", 10);
circle_mc._x = 250;
circle_mc._y = 250;
drawCircle(circle_mc, 250, 0xFFe0AC, 50);
ActionScript 3
var clip:MovieClip = new MovieClip();
addChild(clip);
clip.x = 250;
clip.y = 250;
clip.graphics.lineStyle(2, 0xF89950);
clip.graphics.beginFill(0xFFe0AC, .5);
clip.graphics.drawCircle(0, 0, 200);
Circle dynamically drawn in ActionScript 3.0
24
ActionScript 3 migration cookbook
25
adobe.com/go/as3migration
cookbook: Dynamically set the color of a MovieClip
Task: You need to dynamically set the color of a
MovieClip
.
Solution: Use the
colorTransform
property to set the color.
ActionScript 2
var clip:MovieClip = attachMovie("circle_clip", "circle_clip", 1);
clip._x = 100;
clip._y = 100;
function onStageClick():Void
{
var c:Color = new Color(clip);
c.setRGB(Math.random() * 0xFFFFFF);
}
onMouseUp = onStageClick;
ActionScript 3
var clip:MovieClip = new circle_clip();
clip.x = 100;
clip.y = 100;
addChild(clip);
function onStageClick(event:MouseEvent):void
{

var c:ColorTransform = new ColorTransform();
c.color = (Math.random() * 0xFFFFFF);
clip.transform.colorTransform = c;
}
stage.addEventListener(MouseEvent.CLICK, onStageClick);
cookbook: Dynamically swap MovieClip depths
Task: You need to dynamically swap the depths of two
MovieClip
instances.
Solution: Use the
swapChildren

API.
ActionScript 3
var clip_1:MovieClip = new green_square();
clip_1.x = 100;
clip_1.y = 100;
var clip_2:MovieClip = new blue_square();
clip_2.x = 115;
clip_2.y = 115;
addChild(clip_1);
addChild(clip_2);
stage.addEventListener(MouseEvent.CLICK, onStageClick);
function onStageClick(event):void
{
swapChildren(clip_1, clip_2);
}
ActionScript 2
var clip_1:MovieClip = attachMovie("blue_square", "clip_1", 1);
clip_1._x = 100;
clip_1._y = 100;
var clip_2:MovieClip = attachMovie("green_square", "clip_2", 2);
clip_2._x = 115;
clip_2._y = 115;
function onStageClick():Void
{
clip_1.swapDepths(clip_2);
}
onMouseUp = onStageClick;
Note:
Math.random * 0xFFFFFF
creates a random color.
26
ActionScript 3 migration cookbook
27
adobe.com/go/as3migration
cookbook: Dynamically create and loop through MovieClip instances
Task: You need to dynamically create and then loop through MovieClips.
Solution: Use
DisplayList
and
DisplayListContainer
container methods.
ActionScript 2
var numItems:Number = 250;
var stageWidth:Number = Stage.width;
var stageHeight:Number = Stage.height;
createEmptyMovieClip("container", 2);
container.height = stageHeight;
container.width = stageWidth;
function initClips():Void
{
var clipName:String;
for(var i:Number = 0; i < numItems; i++)
{
clipName = "circle_clip_" + i;
container.attachMovie("circle_mc", clipName, i + 5 );
randomizeClip(container[clipName]);
}
}
function randomizeClip(clip:MovieClip):Void
{
clip._x = random(stageWidth);
clip._y = random(stageHeight);
clip._xscale = random(200);
clip._yscale = random(200);

var c:Color = new Color(clip);
c.setRGB(Math.random() * 0xFFFFFF);
clip._alpha = random(100);
}
function onEnterFrame():Void
{
var c:MovieClip;
for(var i:Number = 0; i < numItems; i++)
{

c = container["circle_clip_" + i];
randomizeClip(c);
}

}
initClips();
ActionScript 3
var container:MovieClip = new MovieClip();
addChild(container);
var numItems:Number = 250;
var stageWidth:Number = stage.stageWidth;
var stageHeight:Number = stage.stageHeight;
function initClips():void
{
var c:MovieClip;
for(var i:Number = 0; i < numItems; i++)
{
c = new circle_mc();
randomizeClip(c);
container.addChild(c);
}
}
function randomizeClip(clip:MovieClip):void
{
clip.x = Math.random() * stageWidth;
clip.y = Math.random() * stageHeight;

clip.scaleX = Math.random() * 2;
clip.scaleY = Math.random() * 2;

var c:ColorTransform = new ColorTransform();
c.color = (Math.random() * 0xFFFFFF);
clip.transform.colorTransform = c;

clip.alpha = Math.random();
}
function onEnterFrame(event:Event):void
{
var c:MovieClip;
for(var i:Number = 0; i < numItems; i++)
{

c = MovieClip(container.getChildAt(i));
randomizeClip(c);
}

}
initClips();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
28
ActionScript 3 migration cookbook
29
adobe.com/go/as3migration
Dynamically created MovieClip instances from the example on previous page
30
ActionScript 3 migration cookbook
31
adobe.com/go/as3migration
cookbook: Play an embedded sound
Task: You need to play a sound contained within your content and set its volume.
Solution: Use the
Sound
,

SoundChannel
,
and
SoundTransform
classes to play and
manipulate sounds.
ActionScript 2
function onSoundComplete():Void
{
trace("sound is completed");
}
var my_sound:Sound = new Sound();
my_sound.attachSound("beep_id");
my_sound.setVolume(50);
my_sound.onSoundComplete = onSoundComplete;
my_sound.start();
ActionScript 3
function onSoundComplete(event:Event):void
{
trace("sound is completed");
}
var my_sound:Sound = new beep_id();
var sTransform:SoundTransform = new SoundTransform();
sTransform.volume = .5;
var channel:SoundChannel = my_sound.play();
channel.soundTransform = sTransform;
channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
Setting the class name for an embedded
sound for ActionScript 3
32
ActionScript 3 migration cookbook
33
adobe.com/go/as3migration
cookbook: Dynamically load and play a sound
Task: You need to dynamically load and play an MP3 file.
Solution: Use the
Sound.load
API to load and control the sound.
cookbook: Dynamically load and display an image
Task: You need to dynamically load and display an image.
Solution: Use the
Loader

class to load the image.
ActionScript 3
var request:URLRequest = new URLRequest("image.png");
var loader:Loader = new Loader();
loader.load(request);
loader.x = 100;
loader.y = 100;
loader.rotation = 20;
loader.alpha = .5;
addChild(loader);
ActionScript 3
var url = new URLRequest("sound.mp3");
var sound = new Sound();
sound.load(url);
sound.play();
ActionScript 2
createEmptyMovieClip("loader", 10);
loader.loadMovie("image.png");
loader._x = 100;
loader._y = 100;
loader._rotation = 20;
loader._alpha = 50;
ActionScript 2
var sound = new Sound();
sound.loadSound("sound.mp3", true);
34
ActionScript 3 migration cookbook
35
adobe.com/go/as3migration
cookbook: Load and read XML
Task: You need to load and read an XML file.
Solution: Use the
URLLoader
API to load the XML and the E4X
XML
API to parse it.
ActionScript 2
function onXMLLoad(success:Boolean):Void
{
trace(this);
trace("Number of Contacts : " + this.firstChild.childNodes[0].childNodes.length);

var firstPerson:XMLNode = this.firstChild.childNodes[0].childNodes[0];
var nodes:Array = firstPerson.childNodes;
var nodeLen:Number = nodes.length;

var node:XMLNode;
var favoriteFood:String;
for(var i:Number = 0; i < nodeLen; i++)
{
node = nodes[i];

if(node.nodeName == "favoriteFood")
{
favoriteFood = node.firstChild.nodeValue;
}
}

trace("First contact’s favorite food : " + favoriteFood);
}
var my_xml:XML = new XML();
my_xml.onLoad = onXMLLoad;
my_xml.ignoreWhite = true;
my_xml.load("contacts.xml");
ActionScript 3
onXMLLoad(event:Event):void
{
var xml:XML = new XML(event.target.data);
trace(xml);
trace("Number of Contacts : " + xml..person.length());
trace("First contact’s favorite food : " + xml.contacts.person[0].favoriteFood);
}
var loader:URLLoader = new URLLoader();
var url:URLRequest = new URLRequest("contacts.xml");
loader.addEventListener(Event.COMPLETE, onXMLLoad);
loader.load(url);
Note: Both examples use the following XML contained in a file called contacts.xml:
<xml>
<contacts>
<person>
<name>Mike Chambers</name>
<favoriteFood>Bacon</favoriteFood>
</person>
<person>
<name>John Doe</name>
<favoriteFood>Pez</favoriteFood>
</person>

</contacts>
</xml>
Note: in the ActionScript 3 example
this
in the
onXMLLoad

function refers to the timeline that contains
the code, while in the ActionScript 2 example,
this
refers to the
XML
object instance.
36
ActionScript 3 migration cookbook
37
adobe.com/go/as3migration
notes:
notes:
38
ActionScript 3 migration cookbook
39
adobe.com/go/as3migration
Online resources and information
ActionScript® 3 migration resources
ActionScript® 3 Migration Center
adobe.com/go/as3migration
ActionScript® 3 Migration Reference
adobe.com/go/as2toas3

Forums
ActionScript® 3 Forum
adobe.com/go/as3_forums
Adobe Flash® Forum
adobe.com/go/flash_forums

Documentation
ActionScript® 3 API Reference
adobe.com/go/flash_docs
Adobe Flash® CS4 Professional Support Center
adobe.com/support/flash/

Designer and developer centers
Adobe Flash® developer center
adobe.com/go/flashdevcenter
ActionScript® developer center
adobe.com/go/flash_as3
Adobe design center
adobe.com/designcenter/
Product technology pages

Flash Player
adobe.com/go/flash
Adobe Creative Suite® 4
adobe.com/go/creativesuite