Migrating from ActionScript 2 to ActionScript 3 Notes

I have been attempting to updated an old Flash CS4 project written in AS2 to the current version of Flash Pro CC, and discovered that it no longer supports AS2. So, you are forced to update your project.

Conversion Strips Scripts

The update process strips out any scripts attached to the instances, which I guess is good. I don’t think I was using any. You don’t get any kind of list telling you that there’s a problem.

New Package Usage Syntax

All my scripts are organized by folder in the dseah package hierarchy, and there are subpackages like dseah.ett and dseah.io. I’m working through compiler errors about packages; notably:

5007: An ActionsScript file must have at least one externally visible definition.

Googling this error brings up advice in wrapping this in a package so you can declare the constructor of your class public. Here’s an example of an old AS2 class:

import mx.services.*;
import dseah.io.Debugger;

class dseah.io.DSWebService {
    [...]   
    function DSWebService(wsdl:String, listener:Object, func:Function) {
        [...]
    }
}

Most of the blathering on StackExchange suggests wrapping everything in a the global package, as in:

package {
    import mx.services.*;
    import dseah.io.Debugger;

    class dseah.io.DSWebService {
        [...]   
    }
}

You will, however, get a NEW error:

1037: Packages cannot be nested.

It took me a while to figure out what this meant, because there is a lot of blather on StackExchange about “autogenerated package namespaces” and “try purging your project cache in FlexBuilder” and even “It works for me.” It actually is another change in AS3 from AS2, and it’s a big DUH: when using the package keyword, you must match the hierarchy and the class names no longer have the path. This is the way other languages work, and it makes sense. It should look like this:

package dseah.io {
    import mx.services.*;
    import dseah.io.Debugger;

    class DSWebService {
        [...]   
    }
}

Then, the error goes away.

Need to explicitly include packages

The built-in Flash classes now have to be explicitly imported.

Changes in Focus and Events

Selection goes away, and is accessible now as the stage.focus property of a DisplayObject (movieclip, textfield).

TextField.onKillFocus and onSetFocus are replaced with general event listener syntax: addEventListener(FocusEvent.FOCUS_OUT, function (e:Event) { ... });

Changes to SOAP

It’s gone, and has been replaced with something new.

setInterval() and getTimer() changes

The callback works differently, accepting now a function reference instead of a closure object + function name string.

Loss of _root

There’s no easy way to grab _root. Working on fix.

Loss of TextFormat.getTextExtents()

Workaround is to make a new TextField, set its text format and text, and pull its textWidth and textHeight properties afterwards. This is possible now that the TextField isn’t a weird Flash object.

WORK ONGOING

… work ongoing to get to an error-free compile, then to see what else has broken. Sigh.