grilldor
01-14-2009, 09: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!
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!