Skip to main content
somascope
Inspiring
December 3, 2007
Question

addChild works with document class, but not timeline-instantiated class

  • December 3, 2007
  • 6 replies
  • 1088 views
I cannot get a sprite to display for me when I create an instance of a very simple class in my FLA file's timeline.
However, when I make the class file the FLA's document class file, it displays just fine.
The task in question is simply drawing a square with the shape class.

Problem example:
My class, Test.as, resides in the same folder as my FLA file - so I don't use any import statement. I'm new to lots of CS3 stuff. Can anyone tell me why this doesn't work and create the shape on the stage? This seems to fail silently without any errors:
var myTest:Test = new Test();

Working example:
Setting the document class to Test.


This topic has been closed for replies.

6 replies

March 7, 2008
You're welcome. But just to leave you with a thought, no matter where a class is instantiated, it cannot be automatically added to a display list. The only time you'll see a class added to a display list without explicitly doing so in code, is when it it is created in the IDE -- and that's something that the compiler somehow handles.
March 7, 2008
You can create an instance of any DisplayObject class and access its members immediately. You can even draw on it if it has a canvas (Graphics) or add child DisplayObjects if it is a DisplayObjectContainer. But you won't see it until you add it to a display list (addChild, addChildAt). And you can use removeChild or removeChildAt to take it off one display list and save it for later, or use addChild, addChildAt to move it to another display list (DisplayObjectContainer).
Known Participant
March 7, 2008
Thanks Raymond - I'm afraid the penny ain't dropping for me, so instead of instantiating the 'Test' class from the timeline and I do it from the document class (as you should) it works a treat with no tweaks. I've done as I think you were suggesting. Created new 'Test' from the timeline, put an empty mc ('holder') on the stage, passed that as a movieclip variable to the constructor and holder.addchild(some graphic) and thats all fine - Still don't understand why one method of instantiating the class is fine the other causes unpredictable results, at a guess the document class route of doing so automatically adds it directly to the display list?
Thanks
March 7, 2008
Wow -- that sounds overly complicated. I don't understand why you would need to pass a "holder" to the constructor. Assuming you're referring to the Test class in this thread, why don't you use this.addChild(some_graphic)


Damon Edwards
Inspiring
December 3, 2007
You are welcome. Don't beat yourself up.
Known Participant
March 7, 2008
Sorry to rekindle this one and appreciate that it's not good practice to have framescripts and classes combined but would love to know why I can access functions in the new 'Test' class on the main timeline without using addChild(myTest) on the main timeline, and yet the 'Test' class is unable to draw a square without it?
Many Thanks
Mikeb
somascope
somascopeAuthor
Inspiring
December 3, 2007
Thanks much - quite the nice and simple thing I was missing here!
Damon Edwards
Inspiring
December 3, 2007
Then you should be able to do this

import Test;

var myTest:Test = new Test();
addChild(myTest);
Damon Edwards
Inspiring
December 3, 2007
so you're trying to just place this on, lets say, frame 1?

Do that like so:

function doThing():void{
var myRect:Shape = new Shape();
myRect.graphics.lineStyle (2, 0xcc0000, 1);
myRect.graphics.beginFill(0xcccccc, 1);
myRect.graphics.drawRect(10, 10, 200, 200);
addChild(myRect);
}
doThing()
somascope
somascopeAuthor
Inspiring
December 3, 2007
You are correct, dzedward, what you write certainly does the trick if just doing things on the timeline. But my issue is that I want to instantiate a class on the timeline, which then creates the shape. I don't want to call a non-class function as your example demonstrates - I want it to be a method that the class's constructor calls from within its class.

Any other thoughts?