Copy link to clipboard
Copied
Hello forum
I'm a little confused with the new addChild / stage methodology of actionscript 3. I'm aware that things have changed quite a bit since as2 I just need a little jumpstart. Thanks for bearing with me...
I have a project.as class which loads a few data files and once all is loaded properly it instantiates a main.as class which again creates various subclases (layouts, controllers, models, ...). Now my point is that various classes are being instantiated without really drawing anything to the stage. Once the first view tries to create a shape or a text it is 2 or 3 instances away from project.as and nothing appears... what am I doing wrong?
- Do I need to save a reference to the stage from project.as which the layout package could make use of?
OR
- Do I need to pass on a reference to stage from project.as to main.as which again passes on the stage reference to its layout files?
I'm looking through tutorials but most of them only show a simplified version of one class only.
Thank you for any advice...
Regards,
sk
Copy link to clipboard
Copied
Your post seems a little vague about the exact relationships between things but here is the general answer that seems to come up often.
People ask a question similar to this:
I have Class A and it contains a sprite called mySprite and I do addChild(mySprite) why can't I see it?
The answer is that because you haven't added the instance of Class A to some DisplayObject display list.
Even if there is nothing in Class A to display other than the sprite instance every step of the way needs to be added to some display list or you won't see any of it. As to exactly which display list you want to add things that is a whole nother matter and really up to the specific design of whatever you are building.
Copy link to clipboard
Copied
Is your 'project.as' file your application class? If so, and it then instantiates another class called 'main.as', nothing main.as does will appear on the stage, unless your project class adds main to it after instantiating it, like so:
var mainObject:main=new main();
this.addChild(mainObject)
(This is assuming your main class extends a DisplayObject class, such as Sprite or MovieClip.)
And then, yes the same logic applies. No classes that your main class instantiates will appear on the stage unless they are added with addChild.
Copy link to clipboard
Copied
Thank you both. I guess that would explain why things don't show up.
A rough insight into my structure:
- project.as
- loads a few XML files
- loads fonts
- loads a sound.swf file
- registers all the data with a data package for easy/global retrieval later on.
- instantiates the main.as class
- main.as
- creates the main MVC triad
- instantiates views which draw objects to the stage
- exampleView.as
- draws a background square to the stage
In my as2 projects I have always created an InstanceManager class in the project.as class where I would register instances (among others the main timeline) that need to be accessible from various classes as the application keeps unfolding itself. My projects usually have 50-100 classes and passing on a stage reference from class to class seems kinda redundant to me.
I now have tried doing the same thing in as3, registering the stage property from my application class 'project.as' in the static class InstanceManager. Then a few levels down (in my exampleView.as class for example), I use the following code to add a background square:
InstanceManager.STAGE.addChild(background);
...and this seems to work as well.
Now will I run into issues with this approach further down the line? Or am I in better hands by following your suggestion of adding main.as and all the views to the displayList with addChild when instantiating them...?
Any advice or thoughts much appreciated!
Thank you again.
Cheers,
sk
Copy link to clipboard
Copied
I agree that it seems silly to pass a reference from class to class to class. So I would probably add Main to the Display list and then just have everybody add themselves to their own display lists.
So when main instantiates ExampleView it adds that instance to its own display list. And when the exampleView instance draws a graphic it adds it to its display list and so on. Seems more orderly to me. But what do I know?