A New Beginning - From Flash 8 to Flash 9
Well incase you have been living in a cave for the last month, you should already know that Flash 9 Alpha is out. Now with it comes along Actionscript 3.0 with many many changes, and so it is a new beginning for pretty much all Flash Developers.
This is (what I hope to be) the first of many tutorials on AS2.0 -> AS3.0 Migration. For further reading on AS2.0 to AS3.0 read the ActionScript 2.0 Migration document on livedocs. So the first topic that has arose while I have been playing around is the restructuring of the MovieClip Class.
A New MovieClip
Okay we all use MovieClips for pretty much everything we do in flash that needs to be displayed. So any change to this class is going to affect us all. Now, what you have to understand with AS3.0, it isnt like a small change, there is a whole OOP re-write. The diffrences change the way you develop in flash (for the better I might add) and generally force you to program in a certain way. For those of you who are familiar with the AS2.0 Object Orientated Programming structure the jump to AS3.0 isnt too bad. You still have alot of the old methods , and all the old properties are still there (and some new ones) but lets get further in to this.
One of the first things I think I should mention is that you dont have attachMovie() or createEmptyMovieClip() anymore. Now this poses a small challenge, how do we create Movieclips by actionscript without these fundamental methods? Well, remember how I said there has been a new OOP re-write? There is now a strict association between MovieClips and classes. Each of your own MovieClips should have its own class to handle it. Which does make sense, how does the MovieClip know how to act if you dont give it a class to tell it so? Okay you could do something like:
var myMC:MovieClip = _root.attachMovie("myID", "myNewName", 10);
myMC.onEnterFrame = function()
{
…
}
Where you have already got a movieclip in your library with the identifier myID, and so it places this movieclip on to the stage. But you may not have realised this yet, but that is very ugly. The new way works like this. Once you have your MovieClip in your library you then need to create a class to handle your MovieClip:
package
{
import flash.events.*;
import flash.display.MovieClip;
public class myClassName extends MovieClip
{
function myClassName ()
{
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(e:Event):void
{
…
}
}
}
Thats an example of a new AS3.0 Class to handle your MovieClip. It looks familiar but there are a few diffrences that is for another tutorial, but for now lets carry on with MovieClip creation. So now we have a Class to handle our new MovieClip. So now we need to link our MovieClip to our class, we do this in very much the same way as we did in Flash 8: (*note* If you didnt create your class before trying to link it to a class it will say “A definition for this class was not found on the disk, so one will be automatically generated on export”. Now this is tempting to do if your not doing anything special with your movieclip. But dont, It is much better practise if you create the .as file your self. It gives you full control over your class.)
So now we have our MovieClip in our Library linked to our Class, So now we need to make an instance of it so it can be used, and placed on the stage:
import myClassName; var myClip:MovieClip = new myClassName (); stage.addChild(myClip);
Now you have myClip as a variable, which is a refrence to an instance of your class, which is linked to your MovieClip in your library. We now add this movieclip to the stage, this tells the stage that this movieclip exists and it needs to be rendered.
Now you have your MovieClip and you have it on the stage, and you have your refrence in Actionscript to it. The only problem now is that the properties have changed… Now this isnt as big of a problem as you may think. They have only been renamed in such a way that if the property had a underscore before it in AS2.0 now it doesnt. eg: myMC._x is now myMC.x and myMC._width is now myMC.width.
With that aside I think you have enough to play with MovieClips in AS3.0!
on July 23rd, 2006 at 4:19 pm
Very nice to see Adobe forcing some order into there programming language.
on December 13th, 2006 at 4:51 pm
Hmm, I agreed until I found you couldn’t class link more than one MovieClip to the same class…
That’s not good!
on March 20th, 2007 at 11:00 pm
one class in package called exectly as actionscript filename (className.as) works fine, but when you added secound class to package I couldn’t linkage it to MC ;/
on March 28th, 2007 at 3:33 am
If “myclip” contains any movieclips with instance names, it will cause an error. It leaves me asking:
“Does AS3 / Flash 9 support nested moviclips with instance names? OR do these movieclips have to be built in ActionScript ?”
on April 27th, 2007 at 4:46 pm
“If “myclip” contains any movieclips with instance names, it will cause an error. It leaves me asking:
“Does AS3 / Flash 9 support nested moviclips with instance names? OR do these movieclips have to be built in ActionScript ?”"
I encounter exactly this error…
is there a solution for this problem?