Hey FlashCoders:

In the app I am building, I have a need to add and remove functionality to an object at runtime--specifically, functionality that will snap the object to a border. I have puzzled on this for awhile, and the method that seems most applicable to me is the Flair pattern described in "OOP with ActionScript" by Hall & Wan. If you don't have the book, the pattern, in short, is a static class that dynamically creates a child object on the class it is modifying, and then adds methods and properties to that object.

My question is: has anyone else used this pattern in AS2 projects? Is it even the best way to achieve what I am trying to achieve? An outline of my SnapFlair class follows below.

Any input you can offer is greatly appreciated, as always,

OK
DAH

SNAPFLAIR CLASS
Implemented as a Singleton

class  SnapFlair {
        private static var _obj:SnapFlair;
        
        private function SnapFlair() {}
        
        /**
        * @param target                 The MovieClip being 'flaired'
* @param source The source of the event that the flair functionality is listening for * @param eventName The name of the event that the flair is listening for
        */      
public function snapOn( target:MovieClip, source:Object, eventName:String ) {
                target.mc.$snapFlair = new Object();
                target.mc.$snapFlair._obj = target;
                source.addEventListener( eventName, target.$snapFlair );
                target.mc.$snapFlair[eventName] = onEvent;
        }
        
        public static function getObj():SnapFlair {
                if (SnapFlair._obj == null) {
                        SnapFlair._obj = new SnapFlair();
                }
                return _obj;
        }
        
public function snapOff( target:MovieClip, source:Object, eventName:String ) {
                source.removeEventListener(eventName, target.$snapFlair)
                delete target.mc.$snapFlair;
        }
        
        public function onEvent( evt:Object ) {
                // do stuff
        }
        
        public function toString():String {
                return "Class SnapFlair";
        }
}


_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to