2008/09/01

When to use the Event.ADDED_TO_STAGE?

form:http://www.flepstudio.org/forum/tutorials/775-event-added_to_stage.html

Here we are again with a new trick of the day (even though I do not consider it as a trick but pure extract of OOP). This one could help us more then once when developing application with Flash CS3.

I am talking about the event Event.ADDED_TO_STAGE.

We need a good example to arrive to the point of this tutorial.

Let us suppose that we have a MovieClip on stage named 'container_mc' and a second MovieClip named 'quadrato_mc'.

We would like to attach another MovieClip placed in library (associated to a class named 'clip') inside container_mc. Up till there everything is fine as we know more then once how to do it: take a look at this article to refresh your memory if needed.

My question to you at this point, would be:

do we need to create the class associated to the MovieClip that we want to attach o shall we let Flash do the job for us to create it ?

Personally, I do think that we should always create it ourself.

First of all, doing so, we do not need to write codes inside the MovieClip as we would write everything in the class keeping full control and keep away from unwanted errors.

The second point is that we can maintain the whole hierarchical tree of our application.

I explain myself better:

And if we wanted to retrieve quadrato_mc placed on stage from the attached clip that is found inside container_mc'

We know that the famous _root does not exist anymore and up till now we have used the technique to pass the value of the stage from the Document Class implementing the building function, from the class associated to the attached MovieClip, a receiving parameter inserted in a property.

It is not easy to explain it by words so let us see a concrete example'
Up till now, we have used this technique:

- a FLA named 'main.fla' into which we have a MovieClip placed on stage with an instance name 'container_mc' and a second one named 'quadrato_mc'


- a Document Class named 'Main.as'

Code:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
} } }


- the class, named Clip.as, associated to the MovieClip, named mc_clip, placed in library

Code:
package {
import flash.display.MovieClip;
public class Clip extends MovieClip {
private var _fla:MovieClip;
public function Clip(fla:MovieClip) {
_fla=fla;
} } }

We attach mc_clip inside container_mc from the Document Class the following way:

Code:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
attachClip();
}
private function attachClip():void {
var clip:MovieClip=new Clip(this);
container_mc.addChild(clip);
} } }

and if we wanted to retrieve quadrato_mc placed on stage from Clip.as:

Code:
package {
import flash.display.MovieClip;
public class Clip extends MovieClip {
private var _fla:MovieClip;
public function Clip(fla:MovieClip) {
_fla=fla;
trace(_fla.quadrato_mc);
} } }


Is it possible that from Clip.as there is not a way to retrieve the stage without having to go through the Document Class'!'The answer is using Event.ADDED_TO_STAGE .Let us see how.The same Document Class:


Code:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
} } }


Clip.as becomes as following:


Code:
package {
import flash.display.MovieClip;
public class Clip extends MovieClip {
public function Clip() {
} } }

If I instance Clip from Main:

Code:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
attachClip();
}

private function attachClip():void {
var clip:MovieClip=new Clip();
container_mc.addChild(clip);
} } }

and in the building function of Clip.as I try to retrieve the parent (which would be container_mc)

Code:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Clip extends MovieClip {
public function Clip() {
trace(parent);
} } }

I obtain a null!When I instance Clip, the building function is looking for the parent to carry out the trace. It can not find it as it has not been inserted yet into container_mc. In fact, container_mc.addChild(clip); is further down the line after I created the instance of Clip. This is why I needed to pass the value of the stage.Instead, if I add to Clip a listener to the event Event.ADDED_TO_STAGE, everything works out fine:

Code:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Clip extends MovieClip {
public function Clip() {
addEventListener(Event.ADDED_TO_STAGE,go);
}
private function go(evt:Event):void {
removeEventListener(Event.ADDED_TO_STAGE,go);
var _root:MovieClip=parent.parent as MovieClip;
trace(_root.quadrato_mc);
} } }

See you soon !

沒有留言: