zzup :P
As we"ve seen, the difference between AS 2 and AS 3 are not just a few, actually I"d say they are many and deep.
The attachMovie method has been removed, therefore I"ll explain how to "attach" a MovieClip via a linkage of the library and we"ll see that it is simple and it shows the beauty of Object Oriented Programming.
I create my FLA and I call it "attach_me".
I create a MovieClip and I call it "mc_clip".
I create a MovieClip inside mc_clip and I call it "mc_dot".
Now I create the Document Class (the Main Class):
Code:
package
{
import flash.display.MovieClip;
public class AttachMe extends MovieClip
{
public function AttachMe()
{
stage.frameRate=24;
var clip:Clip=new Clip();
this.addChild(clip);
}
}
}
and I save the AS file as "AttachMe" (always called as the class"always!).
Don"t let the line var clip:Clip=new Clip(); scare you; now we"ll see who Clip is.
I create another class called "Clip" and I save it in the same folder holding attach_me.fla and AttachMe.as:
Code:
package
{
import flash.display.MovieClip;
import flash.events.*;
public class Clip extends MovieClip
{
private var spring:Number=.1;
private var center:Number;
private var vel_x:Number=0;
public function Clip()
{
this.center=this.width/2;
this.initEvent();
}
public function initEvent():void
{
this.dot_mc.x=this.vel_x;
this.dot_mc.y=this.height/2;
this.dot_mc.addEventListener(Event.ENTER_FRAME,bounding);
}
public function bounding(event:Event):void
{
var acc_x:Number=(this.center-this.dot_mc.x)*this.spring;
this.vel_x+=acc_x;
this.dot_mc.x+=this.vel_x;
}
}
}
Let"s go back to the FLA and:
Right-click on mc_clip in the library, follow linkage and the famous window (where in Flash 8 you used to put the name reference to export the MovieClip with Actionscript) opens.
Let"s activate the option " Export for Actionscript " and 2 text fields become available:
Class
Base class
In Class I put : Clip (we"ll see why later), while I leave Base class as it is.
Now, inside the Clip class, the " this " is exactly referencing the MovieClip "mc_clip" we"re initiating ( vulgarly said to be "attached") from the library.
I launch the swf:
As we can see, inside the Clip class, I call this.dot_mc , dot_mc is in fact a child of mc_clip in the library..
This shows that, even who"s not very familiar with coding, once the MovieClip is initialized from the library via the linkage, we can have anything we like inside it and easily accessible.
Until next time !
Bookmarks