Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > General > Best Practices

Reply
 
Thread Tools Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
Old 06-20-2008, 06:30 PM   #1
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 681
Default [AS3] Of what value be these namespaces?

I've recently acquired the Essential Actionscript 3.0 book and have been reading straight through in my spare time and on the bus. I'm currently about halfway through the chapter on Namepsaces; I have to admit that it's making my head hurt. I can understand the potential value of using a URL string to determine the country of origin of a site's visitor, but in other instances, I'm just not seeing it. It seems to me so far like it's basically a convoluted, roundabout way to control access to variable and function names.

I'm not seeing the value here. Can anyone give me some examples where the use of namespaces made their lives appreciably easier than had they done otherwise? Are there specific best practices involving them? If so, what makes them best practices aside from convention?
__________________
man.mask = mask_mc;

Sigh. The AS3 version just doesn't look at nice as
'man.setMask(mask_mc);'
maskedMan is offline   Reply With Quote
Old 06-21-2008, 10:08 AM   #2
creynders
flash veteran
 
creynders's Avatar
 
Join Date: May 2005
Location: Belgium
Posts: 899
Default

I'm certainly no expert on namespaces and don't know if this is what they are really meant for, but I use them for 2 kinds of things:
1. as qualifiers for event listeners
2. for implementations of state machines

Both can be done in lots of different (and maybe better ??) ways.
I'll give an example of each
ActionScript Code:
private namespace radioButtonNS; private namespace simpleButtonNS; this.addEventListener( someRadioButton, radioButtonNS::onClick ); this.addEventListener( someSimpleButton, simpleButtonNS::onClick ); radioButtonNS function onClick( event : Event ) : void{     // do something } simpleButtonNS function onClick( event : Event ) : void{     //do something }
To me, this is a very clear way to separate the event listeners from the "normal" functions and from each other. Of course, each case has different needs or can be constructed more easily; sometimes I create one event handler which uses a switch statement.

But to me the power of namespaces is more in the second usage.
Let's say we have an button which in all states it reacts to the same events, but not in the same way.
ActionScript Code:
package {     import flash.display.MovieClip;     import flash.events.MouseEvent;         public class SomeButton extends MovieClip     {         private namespace deselectedState;         private namespace selectedState;                 private var state : Namespace;                  public function SomeButton()         {             this.addEventListener( MouseEvent.MOUSE_OVER, eventHandler );             this.addEventListener( MouseEvent.MOUSE_OUT, eventHandler );             this.addEventListener( MouseEvent.CLICK, eventHandler );             state = deselectedState;         }                 private function eventHandler( event : MouseEvent ) : void{             switch( event.type ){                 case MouseEvent.CLICK:                     state::onClick();                 break;                                 case MouseEvent.MOUSE_OUT:                     state::onMouseOut();                 break;                                 case MouseEvent.MOUSE_OVER:                     state::onMouseOver();                 break;             }         }                 selectedState function click( event : MouseEvent ) : void{             state = deselectedState;         }         selectedState function mouseOver( event : MouseEvent ) : void{             //set color red         }                 selectedState function mouseOut( event : MouseEvent ) : void{             //set color orange         }                 deselectedState function click( event : MouseEvent ) : void{             state = selected;            }                 deselectedState function mouseOver( event : MouseEvent ) : void{             //set color cyan         }                 deselectedState function mouseOut( event : MouseEvent ) : void{             //set color blue         }     } }
Of course, this is just an example, this particular case I'd implement differently, since it's such an easy case.

Last edited by creynders; 06-21-2008 at 10:13 AM..
creynders is offline   Reply With Quote
Old 06-21-2008, 04:38 PM   #3
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 681
Default

Thank you for the example. It seems sensible, and a lot more applicable to what I'm used to making than the KidsGame samples from the book where various classes define their own namespaces to (seemingly needlessly?) differentiate their similarly named methods.
__________________
man.mask = mask_mc;

Sigh. The AS3 version just doesn't look at nice as
'man.setMask(mask_mc);'
maskedMan is offline   Reply With Quote
Old 07-03-2008, 01:36 AM   #4
Assertnfailure
as[org].addListener(this)
 
Assertnfailure's Avatar
 
Join Date: Dec 2005
Location: LA, California
Posts: 838
Default

State machine is a good example....

I basically use namespaces on properties/methods whenever I want a level of restriction comparable to internal, without the necessity of keeping the code all in the same package.

I wouldn't waste too much time on namespaces for things like event handlers, because really those should just be private inside the class anyway. Instead of...

ActionScript Code:
radioButtonNS function onClick(evt:Event):void{} simpleButtonNS function onClick(evt:Event):void{}
I'd use...
ActionScript Code:
private function radioButtonClickHandler(evt:Event):void{} private function simpleButtonClickHandler(evt:Event):void{}

Also, namespaces can used for xml nodes too.
Assertnfailure is offline   Reply With Quote
Old 12-13-2008, 07:48 PM   #5
NickZA
Member
 
NickZA's Avatar
 
Join Date: Aug 2007
Location: Ascot, UK
Posts: 67
Thumbs up

ZOMG. This is what I have been looking for.

(in 1960's household advert narration style) "At last, I can arrange my packages they way I feel it's logical to do, without concerns about how that affects internal access! Thank you, ASSERTNFAILURE!"

You are the man/woman/androgyne, delete as appropriate.
__________________
www.visualharmonics.co.uk

Last edited by NickZA; 12-13-2008 at 07:51 PM..
NickZA is offline   Reply With Quote
Old 06-01-2009, 09:37 AM   #6
srivello
Registered User
 
Join Date: Jun 2009
Posts: 1
Default ActionScript 3.0 (AS3) Namespace Demo Here

Great post. I love the idea of assisting developers so they only seeing the methods via intellisense (of Flex Builder) for methods they need to see. I created a new demo to show that off. http://www.blog.rivello.org/?p=422
srivello 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
Traversing Complex XML/Nested Namespaces andrew444 ActionScript 3.0 0 09-07-2008 08:37 PM
Namespaces issue oxk4r ActionScript 3.0 2 07-17-2008 12:54 PM
help with namespaces jszpila ActionScript 3.0 3 04-09-2008 08:59 PM
public namespaces creynders ActionScript 3.0 7 11-14-2007 02:13 PM
auto import namespaces abeall ActionScript 3.0 7 06-30-2006 04:36 PM


All times are GMT. The time now is 08:38 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.
You Rated this Thread: