PDA

View Full Version : Document Class Question



dcampos
June 25th, 2007, 11:29 AM
Hello all,

I have a question about the document class and hopefully someone can explain why the first frame in my fla file is being called twice.

I have a class RS_MovieClip; this class is just to extend functionality of the MovieClip class adding properties and methods I'm used to using.



public class RS_MovieClip extends MovieClip{
//some properties and methods.
}
Now if I set the document class to RS_MovieClip and set the Base Class of a MovieClip in the library to RS_MovieClip.

http://dcampos.reflectionsoftware.net/post_images/docissuekiruppost.jpg

Place an instance of "greenSquare" on the stage. Put a
trace("first frame"); on the first frame.

When I test movie I receive 2 traces.

Whats also really weird is that if I create a new symbol in the library exported as blueSquare, that just has a blue square in it. Then on the first frame put:
addChild(new blueSquare()); I get a blue square on the stage and in the greenSquare instance that is on the stage.

http://dcampos.reflectionsoftware.net/post_images/docissuekiruppost2.jpg

If someone could explain to me why this is happening I would greatly appreciate it.

Thanks.

mathew.er
June 25th, 2007, 04:02 PM
Because you've created two instances of the TRS_MovieClip class. One by setting it as the document class and second by placing it on the stage as a symbol. Remove the one on the stage and it should work as supposed.

dcampos
June 25th, 2007, 04:21 PM
So, defining a document class does not make the document an instance of the class? So if I create an new instance through code in that document it essentially could have changed if I altered the document instance at runtime adding properties or methods?

dcampos
June 26th, 2007, 05:20 PM
A little update: we contact adobe support we are now waiting a call from them. We went through 2 escalations so far so hopefully 3 is a charm. I will post the findings when we get them.

mathew.er
June 26th, 2007, 09:11 PM
When you set a document class, the main timeline is insance of that class... you can do a simple test. Create Test.as file in the same directory as the .fla and set it as the document class
package
{
import flash.display.*;
import flash.utils.*;

public class Test extends MovieClip
{
public function Test () : void
{
trace ( "Document class is: " + getQualifiedClassName ( this ) );
trace ( "Document class has superclass of: " + getQualifiedSuperclassName ( this ) );
trace ( "Document class has parent of: " + getQualifiedClassName ( this.parent ) );

instanceCreated ()
}

private static var instanceCount : int = 0;

private static function instanceCreated () : void
{
instanceCount ++;
trace ( 'So far we have ' + instanceCount + ' instances of Test' )
}
}
}and place this on the first frame
import flash.utils.*

trace ( "On the main timeline I am: " + getQualifiedClassName ( this ) );you should see output like this:
Document class is: Test2
Document class has superclass of: flash.display::MovieClip
Document class has parent of: flash.display::Stage
So far we have 1 instances of Test
On the main timeline I am: Test2which clearly reveals the situation. If you wouldn't set the document class, you would just see something like
On the main timeline I am: Untitled_fla::MainTimelinealso if you place a breakpoint on the main timeline and in Test's costructor, you would see that the on in costructor is in Test$iinit and the one on the frame is really just on frame actions of Test/::frame1

dcampos
June 26th, 2007, 11:20 PM
I uploaded and example of what I am talking about. Let me know what you think.

dthought
June 26th, 2007, 11:42 PM
I didn't think that a Document-level class was also allowed to contain instances of itself... for example, if you try to add



var myClip:RSMovieClip = new RSMovieClip();


Flash chucks a hissy fit (invalid data... pretty vague error, actually). Perhaps by having a Library class which is of the same class as the document is an interesting work-around, but isn't supposed to be allowed, hence the weirdness?

devonair
June 27th, 2007, 12:16 AM
Basically the document class is your application (website, ria, what-have-you). When you think about what you're trying to do, it leads to a strange logic loop.. That is, if I add an instance of my application to my application, wouldn't that inner application also have an instance of the application? And that inner inner application would have an instance and so on and so on... Also, while it's possible, it really isn't necessary or recommended to add script to the first frame of your movie when using a document class, as that's what the document class is for.. It seems you're trying to force AS3 to behave as AS2 (I see a lot of people trying to do that), but it's really a whole new paradigm that requires you to think outside the box (yes, I'm using stupid corporate buzz words).. If you want to compare AS3 to AS2 think of it this way.. In AS2 you would write maybe 20% of your script in the first frame of the .fla and 80% in external class files.. In AS3 that 20% that used to be in the first frame is now contained in the document class.. That's not 100% accurate, but it's a decent comparison..

EDIT.. Adding an instance of a document class to the document class is kinda like this:
package {

public class LoopClass {
public function LoopClass() {
var myLoop:LoopClass = new LoopClass();
}
}
}

dcampos
June 27th, 2007, 12:41 AM
ok, I was think that when you specified the document class the application extended that class. I would have to say I am guilty of trying to force as3 to behave like as2. I already extend the MovieClip that have methods/properties like as2. Makes for easy code conversion :smirk: