Greetings to all.
What I will show you next will not be classified as good norms in Actionscript development. But, view the numbers of Flash Designers in this community, it will be certainly useful.
I often have been asked for help on how to handle the timeline of MovieClip placed on stage or of a nested MovieClip in another one. Specially, on how to be able to refer to the
Document Class from the MovieClip on stage or from the nested one inside it.
I hope that this example will help you to understand better this communication in between the timeline and the Document Class, although you should remember that it is always best to write the codes only in the Document Class or, if really you can not manage it, to write the code on the main timeline and not all over the application.
Let us get into it… I create a FLA and save it as ‘main.fla’.
Into it, I create a MovieClip and place an instance on stage named ‘clip_mc’.
This MovieClip consists of an animation of more or less 30 frames which will move it from left to right.
I create a Document Class, an AS file saved as ‘Main.as’, implemented the following way:
Code:
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main()
{
clip_mc.stop();
}
public function PlayClip():void
{
clip_mc.play();
}
public function aumentaY():void
{
clip_mc.y+=60;
}
}
}
As we can see, we have the main building function (public function Main) which stops the timeline of ‘clip_mc’ placed on stage.
Also, we have two public methods/functions which can be called from outside the class:
- once called, PlayClip will start the timeline of ‘clip_mc’ placed on stage
- once called, aumentaY will increase the y property of ‘clip_mc’ placed on stage
Let us now see how to call those two methods from the timeline:
On the first and unique keyframe of the main timeline, I write PlayClip();.
Previewing the SWF, I realise that ‘clip_mc’ is moving right, meaning that its timeline has been started…by who? From the method PlayClip();. Doing this way, I called a function implemented in the Document Class. This example will take away any doubts. Writing in the Document Class is the same as writing on the main timeline.
In fact, the call PlayClip from the timeline seems to have been called from the timeline itself.
Said that, what would happen if we needed to call a method of the Document Class from the last keyframe of the clip_mc timeline? How would we do it?
I tested it as followed:
First I tried to write on the last keyframe of clip_mc: trace(this.parent); and the output was [object Main]. This means that the main timeline of our main.fla is an instance of the Main class that we created. So, logically if I would write:
this.parent.avanzaY(); Flash should call the method avanzaY implemented in the Document Class…but…it is not this way. In fact if we preview it, Flash returns an error that this.parent does not have a method called avanzaY (strange isn’t it)…
So, I went on using the ‘universal variable’:
var m:*=this.parent;
m.aumentaY();
I created a variable which can hold any type of value/data and assign to it, this.parent.
I then call avanzaY using the variable m.
Source files: