If a class extends a dynamic class in AS 3.0 then the new class doesn't become dynamic too, so you have to make the new class dynamic explicitly that is why flash gives you error.
The idea was born from a topic in the FlepStudio’s forum in which a user was in difficulty using the logic of my tutorial about the preloader with Flash CS3 .
The difficulty was mainly due to this situation:
- the preloader.fla has a Document Class Loading.as
- prova.fla creates prova.swf. Inside the library of prova.fla we have a MovieClip (clip_mc) associated to a class (Clip.as) and inside this MovieClip, we have another MovieClip named "tavola_mc".
- Loading.as loads prova.swf and once the loading complete, creates an instance of a class Chiama_Clip.as which itself an attachMovie of "clip_mc" placed in library of prova.fla (therefore using the associated class: Clip).*
Once understood the logic of the tutorial about the preloader of FlepStudio, it is not difficult to call that MovieClip and create an instance of it.
But only if this MovieClip does not contain any other object.
Trying to insert another MovieClip (tavola_mc) Flash returns an error and says that it is not able to assign a new property to Clip. As child MovieClips are properties of the parent MoveClip and since Clip.as extends the MovieClip class, the Flash error has left me surprised.* The MovieClip class is dynamic and it can create properties in runtime. But Flash tells me the contrary.
Therefore, we will see in this example how to arrive to attach on stage "clip_mc" but we will also see how to resolve this annoying error given by Flash.*
Let us look at it" I clearly will not repeat the whole logic of the preloader.
Who did not know about it, can see it in this article and can download it from the section downloads.
I will start to the point in which Loading.as finishes to load the external SWF in the function "completato":
in this method I create an instance of the Chiama_Clip class and pass it the value of the project"s root (preloader.fla) and the root of the loaded SWF:Code:private function completato(event:Event):void { preloader_mc.stopRun(); addChild(loader); var sdsd=MovieClip(loader.content) var chiama_clip:Chiama_Clip=new Chiama_Clip(this,sdsd); }
var sdsd=MovieClip(loader.content);
var chiama_clip:Chiama_Clip=new Chiama_Clip(this,sdsd);
The Chiama_Clip class is implemented the following way:
it receives the values from Loadin.as so to be able to tell the loaded SWF to attach "clip_mc" placed in its library and associated (via linkage id) to the Clip.as class:Code:package { import flash.display.*; import flash.events.*; import flash.text.*; public class Chiama_Clip extends MovieClip { private var _fla:MovieClip; private var _miaroot:MovieClip; public function Chiama_Clip(fla:MovieClip,miaroot:MovieClip) { _fla=fla; _miaroot=miaroot var mc1:Clip=new Clip(); _miaroot.addChild(mc1); mc1.x=100; mc1.y=50; mc1.tavola.tabled.text="Scrivi Fatto"; mc1.tavola.wait.gotoAndPlay(2); } } }
Back to Chiama_Clip.as, we see that an instance of Clip.as is created and added to the loaded SWF (prova.swf)Code:package { import flash.display.MovieClip; import flash.display.Sprite; public dynamic class Clip extends MovieClip { public function Clip() { trace("La Classe Clip è Istanziata ") } } }
var mc1:Clip=new Clip();
_miaroot.addChild(mc1);
it then refers to "mc1" to access and be able to interact with all the objects inside it.
Nothing special about it if not for the fact that we do know now how to attach a MovieClip placed in library of a loaded SWF.
But if we do take a closer look at the definition of Clip.as class
public dynamic class Clip extends MovieClip
We notice that there is the new word "dynamic".
As said first, the Clip class should already be dynamic by itself as it extends the MovieClip class and so should be able to create properties in runtime.
However, if we remove that word "dynamic" it does not happen that way.
I can say to have resolved completely this Flash error although I cannot understand the why of it.
I personally think that the Clip class loses its dynamic in the following line of the Chiama_Clip.as:
var sdsd=MovieClip(loader.content);
In which the content of "loader" is forced to be of type MovieClip (as if it would not be a 100% true property of MovieClip)
So, as Clip.as is associated to a MovieClip placed in library on the loaded SWF (placed itself in "loader"), Clip at that moment loses its dynamic side and we need to give it back to it via the class definition using the attribute "dynamic".
To understand best, you can download the complete files of this example in thedownloads section on this site.
See you again soon!
Last edited by Flep; 28-08-08 at 05:42.
If a class extends a dynamic class in AS 3.0 then the new class doesn't become dynamic too, so you have to make the new class dynamic explicitly that is why flash gives you error.
hmm, do you mean, if i extend MovieClip i do not inherit the dynamic ?
That's strange...
anyway thx for the tip
where can i download the files for this tutorial, its under wot name in the download section ?
There are not.
Also, you should use Event.ADDED_TO_STAGE into Chiama_Clip class, so you do not need to pass any value.
Have a look:
Event.ADDED_TO_STAGE
I think you are right about where Movieclip loses dynamic quality.
Have you tried this: var sdsd=MovieClip(loader.content) as MovieClip;
?
http://davidjmcclelland.com
Bookmarks