I recently came across a situation where I was forced to convert a working project in Flash 9 AS3 to Flash 8 AS2. I wanted to share some of my experiences to help other weary programmers in the same predicament.
Before I begin, I must stress to you, if there is any way you can avoid downgrading your code, you should avoid it! If it’s a matter of making two systems communicate, there are countless reasons that it would be better to upgrade your code from AS2 to AS3. There are even ways to host your AS2 code in an AS3 wrapper. In my case, an important requirement (that the deliverable absolutely must be AS2) was left out of the original scope of work due to a misunderstanding but anything that doesn’t cause project stoppage should be considered carefully.
I must also preface this article by reminding the reader that there is no 1-to-1 conversion from AS3 to AS2 (nor the other way around). There are loads of features added to Flash Player 9 that simply don’t exist in Player 8. Some things you’ll be able to convert with a simple find and replace. Some things you’ll need to re-create in AS2 or re-think completely. Some things you just won’t be able to do. This article only covers the stuff I ran across while converting my files and is by no means comprehensive. However, I hope it gives you a good starting point to work from.
Converting the document class
Since you’re a smart, cool AS3 programmer with a good sense of humour, I know you use a document class in your FLA files or a main application class for your FlexBuilder project. Right? Good. Of course, AS2 does not support document classes and all code must be run from the timeline. You have a couple of options here. You could move your document class to the timeline and remove the class specific portions of the code. Perhaps a quicker and easier solution is to modify your document class to take a reference to the root. So you have something like this on the first frame…
var documentClass:MyDocumentClass = new MyDocumentClass(this);
Just keep in mind that your document class will need to store a reference to the root that’s passed in as well as make references to things that were on the stage. For example…
public function MyDocumentClass(mc:MovieClip) {
this._mc = mc;
this.thingOnStage = _mc.thingOnStage;
}
Cleaning up syntax errors
The first step to take once you can compile your code is to run it and fix each compile time error one at a time. To save you the trouble of some of the more common ones I found, I created this list of common syntax problems.
Search and replace:
- void -> Void
- int and uint -> Number
- DisplayObject, DisplayObjectContainer, Sprite -> MovieClip
- public class -> class
- internal -> public
- protected -> private
- override -> (nothing)
- mouseEnabled -> enabled
- [obj] as [type] -> [type]([obj])
- [obj] is [type] -> [obj] instanceof [type]
- const -> var (or optionally, to implicit getter)
- Add underscores before MovieClip variables. e.g.: x, scaleY, alpha -> _x, _yscale, _alpha including:
- alpha -> _alpha
- x,y -> _x, _y
- scaleX,Y -> _xscale, _yscale
- mouseX,Y -> _xmouse, _ymouse
- visible -> _visible
- currentFrame -> _currentFrame
- totalFrames -> _totalframes
Add:
- import statements for classes within the same package (not required in AS3)
Remove:
- import statements for AS3 packages (e.g. flash.display.* )
Convert:
- _xscale, _yscale, _alpha – Convert from decimal to integer (0.2 becomes 20).
- Convert Timer to setInterval
- Convert Loader to MovieClipLoader
- Remove package declaration but copy the package name and put it before the class name. Also remove closing } from the end of the file.
e.g. :package com.mysite.package { public class Foo { //... } }becomes
class com.mysite.package.Foo { //... }
Other tips
Events are a huge part of AS3 development and Player 9 supports a fully event-driven development style. Unfortunately, AS2 is heavily lacking in this department and while events can be recreated in AS2, it lacks method closures and bubbling that make AS3 so nice. To downgrade, I did two things. First, I recreated the event dispatcher in as2 and second, I made heavy use of event callbacks and the Delegate class.
com.mimswright.events
In order to change as little of the event code as possible, I looked back to previous AS2 projects where I had used Danny Patterson’s EventBroadcaster classes. These classes worked roughly the same as the EventDispatcher class in AS3. I took the liberty of brushing up the code and making it look as much like the AS3 EventDispatcher as I could. I’m making it freely available for use.
event callbacks
Most of the events you’ll probably be using must be triggered by callbacks in AS2. That is, you define a function that gets called when an event takes place like
onMouseMove = function ():Void {
//do something
}
Keep in mind that in AS2 there are no method closures as there are in AS3. That is, a function isn’t necessarily executed in the scope that it exists in. In even simpler terms, a function doesn’t know what object it’s associated with. Because of this, code such as button.onRelease = object.handleClick; executes the handleClick function from the button context and my not work properly. Fortunately, AS2 code can make use of the mx.utils.Delegate class – specifically, the Delegate.create() method. This artificially maintains the context in which the function was supposed to be called. So your code should look like button.onRelease = mx.utils.Delegate.create(object, object.handleClick);
I hope this has been a helpful start for you poor souls who are faced with the challenge of down-converting code. Good luck to you and feel free to add your experiences to the comments.
[...] dispatchEvent()™ Collective thoughts of the New York Flash community « Downsampling Flash – Converting AS3 to AS2 [...]
I had to downgrade once because the projector program I was using doesn’t work in AS3 and I only figured it out after completing the application.
I’m curious, why did the client require that the Flash “absolutely must be AS2″?
It had to do with banner ad trafficking.
Why haXe had not used?
So it had a lot more to do with the fact they wanted to use Flash 8 or lower than the fact the wanted to use AS2…?
The AS2 event dispatcher project you provided was really promising until I realized that the MouseEvent doesn’t actually work like AS3 :P does this mean that I’d have to actually manually dispatch the event from a traditional onPress?
Nice work btw!
Downgrading AS3 to AS2… dude.. you are a warrior
[...] Sometimes I forget the details of whether my Model should know about my Controller and that sort of thing. I found this little MVC cheat sheet on the internet that got me straightened out in a jiffy with step-by-step instructions. The page is actually the lecture notes from one of Colin Moock’s presentations circa the Essential AS 2.0 days but it’s still very useful. Those of you struggling with the lingo, replace Observer with EventDispatcher and ignore the junk about attachMovie. God, how did we ever write AS2!? [...]
this is fantastic. Since last five days I was trying on this but due to this page I hope I will definetly do somting more than what I think. Thank You
Guys, think of FlashLite. Export for mobile platforms still only AS2.
I want to inport AS3 in AS2, not to convert…. :)