Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > General > Best Practices

Reply
 
Thread Tools Rate Thread Display Modes
Old 01-14-2009, 09:25 AM   #1
grilldor
EPISODE17
 
Join Date: Aug 2007
Location: Montreal, Qc
Posts: 35
Send a message via MSN to grilldor Send a message via Skype™ to grilldor
Red face Where to put addchild()? + Where to put event handlers

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 :
ActionScript Code:
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 :
ActionScript Code:
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 :

ActionScript Code:
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!

Last edited by grilldor; 01-14-2009 at 09:28 AM..
grilldor is offline   Reply With Quote
Old 01-14-2009, 04:12 PM   #2
bluemagica
Senior Member
 
Join Date: Feb 2008
Posts: 204
Default

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!
bluemagica is offline   Reply With Quote
Old 01-14-2009, 05:17 PM   #3
grilldor
EPISODE17
 
Join Date: Aug 2007
Location: Montreal, Qc
Posts: 35
Send a message via MSN to grilldor Send a message via Skype™ to grilldor
Default

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)
grilldor is offline   Reply With Quote
Old 01-19-2009, 02:05 AM   #4
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

for the background manager, it doesn't really matter but if you ask me, I'd do like you show above:
ActionScript Code:
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

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

also...pick one "on" or "Handler" but not both.
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Menu Help - Event Handlers MSR ActionScript 2.0 3 11-13-2006 09:36 PM
MovieClip event handlers stiffel Buttons inside Selfminded ActionScript 2.0 2 05-16-2006 08:49 PM
Event Handlers & Classes Scope loudsox ActionScript 2.0 2 01-20-2006 03:51 PM
Event handlers for dynamically loaded movieclips? rdegnan ActionScript 1.0 (and below) 6 01-08-2003 04:51 PM
Event handling with Listeners - learning from Java robertpenner ActionScript 1.0 (and below) 2 08-09-2001 12:36 AM


All times are GMT. The time now is 03:25 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.