Big Updates for Flash (Player and IDE) →← 3D Painting w/ Papervision

Migrating from AS2 to AS3

Before we get into the nitty gritty of my post, let’s answer the age old question:

Why choose AS3 over AS2?

The player has made a dent in the market already. It helps that MySpace and YouTube both require Flash 9. It’s far faster. The error messaging is much better, meaning when something breaks you’ll actually be able to diagnose why (as opposed to the old guess and check method).

XML is natively integrated — it’s so easy to work with XML now, it’s unbelievable. Loading actually makes sense. You can read any kind of byte you want.

And that’s just the tip of the iceberg.

The only downside — and it’s a steep downside at first glance — is that ActionScript developers need to get comfortable with a new, strict, *true* programming language. The learning curve seems scary… until you dive in.

Most of the designers here are pretty familiar with AS. Some of them have a basic understanding of AS2, grasping the concepts and uses of OOP even if they don’t entirely get how to write code. All of them are a little terrified of ActionScript 3. This presents a bit of a problem — sometimes designers will want to create a simple example for a coder to rebuild better/stronger/faster for the final deployment of a site.

To empower our team to learn a little about the basics of AS3, I wrote this doc up for them. Now, I share with you. Click on to read my rant.

A lot of questions are being asked about AS3 people. A lot. You have every reason to be frightened. We were frightened, too. But it’s okay. AS3 has replaced all of the old annoying quirks with a slew of new ones. There’s lots more to be frustrated over, and the best part is you don’t even know what it is yet.

Oh learning.

I’ll try and cover as much as I think you’ll need to know in as simple a way as possible. Which isn’t much.

No More _ (as in underscore).

myClip._x = 500; // bad
myClip.x = 500; // good

alpha, x, y, scaleX, scaleY, rotation, etc. This is consistent for any Display Object (hey kids, it’s a key word!). A display object is an object that displays. On the screen. You have your basics: MovieClip, TextField, SimpleButton, Video. Then your new guys, like Sprite (sorta like a graphic symbol on steroids), Video, Bitmap. This sort of thing is explained far better than I can describe here.

Oh yeah, nothing is 100 based anymore… it’s all 0 thru 1. So if you want 50% alpha:

myClip.alpha = .5;

_currentframe is currentFrame.

_parent is parent

You get the idea.

Controlling the Timeline
gotoAndPlay, gotoAndStop are radically, radically different. They now look like this:


myClip.gotoAndPlay(2);
myclip.gotoAndPlay("LABEL_FOO");

Gah! So hard! Right?

No more _root

And I’m not telling you how to access it. You’ll just mess up all our beautiful code.

(myClip.root will give you that specific SWF’s root. I think.)
(myClip.stage will give you the root root root timeline. I’m pretty confident that every single clip in Flash will share the same stage property.)

Strict variables

var n = 0; // flash hates you, and it will tell you so.
var n:Number = 0; // flash might just buy you a burger on wednesday. don't get your hopes up.

The obvious ones are String and Number. Whenever you want to reference a movieclip, you need to import the MovieClip class, like so:

import flash.display.MovieClip; // you only need this once.
var zanderClip:MovieClip;

Methods R Out
onEnterFrame is gone. onRelease, onRollOut, etc, etc, etc. startDrag and stopDrag are still there, but that’s about it. Everything runs off of EventDispatcher now (that fancy schmancy thing we use on this side of the office to make it seem like we know more than you about something). Basically all event dispatching is is blindly broadcasting something has happened. It’s up to you to define Listeners, which will react to events. This is only slightly more complicated than the “old” way.

old way:

myClip.onRelease = function()
{
// now we're in the scope of myClip. that sucks. what if we have variables outside of myClip? now we have to call _parent? no wai.
}

new way:


import flash.events.Event;
import flash.events.MouseEvent;


myClip.addEventListener(MouseEvent.MOUSE_UP,_onMouseUpHandler);


function _onMouseUpFunction($evt:MouseEvent):void
{
// we're still within the original scope, but we know that myClip just was clicked on.
};

Note a couple of things. void is the return type of the function — what it would pass back to another function if called. Since this isn’t returning anything, void it is. $evt is an argument, you all know what that is, right? It’s of type MouseEvent. Can you figure out why? :)

There are lots and lots of events you can listen to, and you can assign about any event to just about any DisplayObject, at least for your purposes. Here’s a bunch that you’ll want to toy around with.

MouseEvent.CLICK
MouseEvent.DOUBLE_CLICK
MouseEvent.MOUSE_DOWN
MouseEvent.MOUSE_MOVE
MouseEvent.MOUSE_OUT
MouseEvent.MOUSE_OVER
MouseEvent.MOUSE_WHEEL
MouseEvent.MOUSE_UP
MouseEvent.MOUSE_DOWN
MouseEvent.ROLL_OVER
MouseEvent.ROLL_OUT


Event.ENTER_FRAME

In addition, there’s a long list of ActionScript 3 bloggers who’s working should help make the migration far smoother. In particular, gskinner and senocular have tons of examples as to the how and why.

August 10th, 2007  by Stephen  /  31 Comments

Comments on “Migrating from AS2 to AS3”

  1. This article is printed out and hanging in a frame on my office wall.
    Well done. Scared is the exact word I would use.

    thx.

    firmtofu on August 10th, 2007 at 11:56 am
  2. […] BigSpaceship made a post about some more AS2 to AS3 migration tips.  These are relatively simple, but as the post states, it was aimed at some of their designers who were terrified of AS3. […]

    More Migration Tips | reintroducing.com Blogging Receptacle on August 10th, 2007 at 1:14 pm
  3. You forgot to mention the big change. Not being able to attach ActionScript to movie clips or buttons. It’s the singe worst change in the program. Really dumb if you ask me. The language is just as hard as CSS and there’s some good reasons to learn that rather than a proprietary language that changes at the whim of the manufacturer dramatically with every version.

    Ray Villalobos on August 10th, 2007 at 1:58 pm
  4. You’re right Ray, and you have a lot of valid points. For everyone’s reference, code is restricted to the timeline and ActionScript (.as) classes now. But there’s a reason for this, and it’s one I think most will understand and appreciate in time. I won’t tell you how many times I’ve confused myself in the past simply by placing code on the instance of a movieclip. It’s already complex enough to keep track of what’s where in your code without being able to arbitrarily put code anywhere… at least now there’s some sembelence of order. Granted, the code can still become messy, but it’s an attempt at order where there previously was none.

    As for your argument against the new language, I frankly don’t see any discernible difference between Adobe altering their specifications versus Microsoft and Mozilla interpreting W3C’s specifications in [sometimes] radically obtuse ways. There are pros and cons to a plug-in, sure, but that debate is for another post another time.

    Jamie on August 10th, 2007 at 2:10 pm
  5. You are slightly mistaken on one point, Ray. The virtual machine which processes it is technically open-source, and ActionScript is part of the ECMAScript standard, which JavaScript is also based on. So the changes are not at the whim of the manufacturer, but carefully planned and considered based on programming standards, ease of use and architecture, and above all, performance.

    The Minister on August 10th, 2007 at 2:24 pm
  6. Not to sound like I’m naysaying or anything, but Youtube and Myspace do not require Flash Player 9. Video on Youtube works fine with 8, as does the music player on Myspace.

    Matt Wright on August 13th, 2007 at 9:05 pm
  7. One that threw me off was addChild. You no longer use attachMovie to place things on the stage. Also, importing XML documents is much easier now. Now if I can just figure out what all this OOP stuff is about! ;)

    David M. on August 14th, 2007 at 12:57 am
  8. you gave me “envy” to test AS3. thx :)

    M.Lassalvy on August 15th, 2007 at 9:16 pm
  9. […] BIG SPACESHIP LABS / » Migrating from AS2 to AS3 (tags: as3 as2 migration flash actionscript reference) […]

    izms » Blog Archive » links for 2007-08-16 on August 16th, 2007 at 6:21 pm
  10. […] Voor de designers onder ons: hier een hele handige handleiding om een snelle schakel te maken […]

    Van AS2 naar AS3 voor Flash Designers on August 21st, 2007 at 4:53 pm
  11. What a great read, Thanks!! :)

    c.moore on August 22nd, 2007 at 1:41 am
  12. The learning curve does seems scary at first. But I now I find myself loving and actually find AS 3 to be easier to use than AS 2. It’s just a matter of getting comfortable with the changes. Thank god they got rid of the scope issue in AS 3. No more stupid Delegate!

    Ben on August 22nd, 2007 at 4:30 pm
  13. really, really useful… THanks !!

    liuhuan on August 25th, 2007 at 7:57 pm
  14. […] SThe deadly challenge. Big Spaceship wrote about their worries on migrating on their Development blog, everyone seems so cautious about the switch myself include, it is a terrifying thought moving into the realms of the unknown. But after following along some videos on Lynda I can see how easy adobe has made it and some of the little things that have changed that upset and confused me for so long. […]

    Migrating from AS2 to AS3 and an AIR extension at Start on August 26th, 2007 at 10:07 am
  15. […] My recommendation, get your AS3 learn on. See what BIG SPACESHIP  thinks of AS3 here.  Big Spaceship just recently got added to thefwa’s hall of fame – one of three agencies all time.  […]

    Flash9/AS3 Market Now At 91% Availability « [ draw.logic ] on September 4th, 2007 at 5:58 pm
  16. Good stuff except for this:

    myClip.addEventListener(MouseEvent.MOUSE_UP,_onMouseUpFunction);

    You can call your event handlers whatever you want but the way it is been done so far is to name them : onMouseUpHandler rather than _onMouseUpFunction to avoid the _ and to describe what it is, a handler.

    Also yes some people are still ysing onMouseUp etc which cause very annoying compiler warnings. Naming them [event]Handler solves all kinds of confusion and problems.

    Ryan [draw.logic] on September 5th, 2007 at 2:31 am
  17. Using Handler instead of Function is good form. Excellent point Ryan. I’ll edit the original post.

    Jamie on September 5th, 2007 at 2:47 pm
  18. Thank you very much

    Geert on September 11th, 2007 at 6:44 am
  19. […] http://labs.bigspaceship.com/blog/?p=61 […]

    twenty3design.co.uk » Blog Archive » Oh I see…AS3 is totally different on September 13th, 2007 at 9:52 am
  20. Thanks for the article! I tried to just dive into AS3 today and almost puked when my on(release) events weren’t working. This makes a bit more sense now! :)

    On another note… if you wanted to add a MOUSE_UP event to a button embedded in a movieClip and you want its action to affect something on the main timeline, do you still do it the old way, with dot syntax? For example… root.mc.gotoAndPlay(”start”);

    J Yo on September 14th, 2007 at 10:07 pm
  21. It’s definitely not worth puking over. Right?

    Dot syntax still works, but you can’t access the root timeline via _root. No more _global, no more _root. You’ll have to be craftier setting up ways to access different movieclips at different scopes. :)

    (myClip.root should get you the root though).

    Jamie on September 15th, 2007 at 2:57 pm
  22. […] Migrating from AS2 to AS3 […]

    `Migrating from AS2 to AS3 » ideaography` on September 24th, 2007 at 7:54 pm
  23. Thanks for the great overview. A great first glance at whats different. The event listeners were the biggest transition for me.

    hebchop on October 2nd, 2007 at 12:18 pm
  24. I better go open a book. I can’t even point a button to a movie clip anymore!

    Justin(pusha) on October 6th, 2007 at 8:21 pm
  25. Beliy Kolya

    Beliy Kolya on October 30th, 2007 at 9:36 am
Next →