PDA

View Full Version : Where to put addchild()? + Where to put event handlers


grilldor
01-14-2009, 08:25 AM
Its been a while i've been asking myself what was the best practise about this and having no teacher or book to find an answer. Im asking you guys.

This is about AS3 btw :

First question :
When I build fairly complex websites, I often build classes by passing the document class across the constructor, so each class has its reference to the "root". (side question : is this good design?).

Now, say I create a new sprite, then add many other stuff to it (other sprites, textboxes, etc)

Question : should I addchild BEFORE, adding these or should I addChild AFTER ading them? What is the difference, if any?

Simple example 1 :

public MyConstructor(root:DisplayObjectContainer)
{
var sprite1:Sprite = new Sprite();
var textbox1:TextBox = new TextBox();
textbox1.text = "blabla"; // Same thing here, edit text before or after addchild?
sprite1.addchild(textbox1);

var sprite2:Sprite = new Sprite();
// sprite2.graphic... draw something on it

root.addchild(sprite1);
root.addchild(sprite2);
}


VS

Simple example 2 :

public MyConstructor(root:DisplayObjectContainer)
{
var sprite1:Sprite = new Sprite();
var sprite2:Sprite = new Sprite();

root.addchild(sprite1);
root.addchild(sprite2);

var textbox1:TextBox = new TextBox();
sprite1.addchild(textbox1);
textbox1.text = "blabla"; // Same thing here, edit text before or after addchild?

// sprite2.graphic... draw something on it

}


So the question is, where is it BEST practise to addchild initally? right after creating your displayobject, or after adding stuff to it. My guess is that it doesnt matter

Other question :

Say i have a background manager class (extends NOTHING).

Class has a reference to the main stage using the constructor(root) method.

What is the best way of handling a RESIZE event here?
1- Doing it within the class, listening to root.stage.addEventListe...
2- Doing it within the root ex :


var backgroundManager:BackgroundManager = new BackgroundManager(this);

this.addEventListener(EVENT.RESIZE, onResizeHandler);

private function onResizeHandler(e:Event)
{
backgroundManager.resize(); //(public function that resizes the background)
}


Again, they are subtle differences, but they bug me !!

Thanks for your help guys!

bluemagica
01-14-2009, 03:12 PM
1) Forget root, try using parent or stage! it will get you better flexibility of design!
2) Do addChild last of all! its a better practice and flash is better prepared to handle the children!
3) If you are gonna handle resize or such events which affect the entire movie as a whole(even if you resize the background, it still affects the movie), then is better you do it from the main document class!

grilldor
01-14-2009, 04:17 PM
thanks!!
But concerning root. Im not refering to DisplayObject.root, my "root" is more like a "working layer". The actions (addchilds) of the class to which i pass this displayobject are done on it.

I guess its a bit like displayobject.parent, but since BackgroundManager is not a displayobject, i dont have access to such a property.

Do you think its a good idea to not make the BackgroundManager a Sprite and work on an external object? (has-a instead of is-a debate i guess)

Flash Gordon
01-19-2009, 01:05 AM
for the background manager, it doesn't really matter but if you ask me, I'd do like you show above:

var backgroundManager:BackgroundManager = new BackgroundManager();
this.addEventListener(EVENT.RESIZE, resizeHandler);
private function resizeHandler(e:Event) {
backgroundManager.resize(width, height);
}


the reason being is what if you wanted to set the background to a certain width and height? If the BackgroundManager's assumes it should always resize to the stage.stageWidth and height, then you can't control this. With what I showed above, you can explicitly set a width and height for the BGManager something other than a set value. so for instance


backgroundManager.resize(Math.min(stage.stageWidth , 955),...);


also...pick one "on" or "Handler" but not both.