So is this still out of the question... 


///
///     IN FLA
///

var box = this.attachMovie ( 'box', 'box',  this.getNextHighestDepth() );

jgButton.initializeMovieClip ( box );
box.data = 'this is my data';

box.showEvents( 'onPress', 'onRollOver' );
box.addEventListener ( 'onPress', mx.utils.Delegate.create( this,
onButtonEventHandler ) );
box.addEventListener ( 'onRollOver', mx.utils.Delegate.create( this,
onButtonEventHandler ) );
function onButtonEventHandler ( evt:Object ) 
{ 
        trace( newline );
        trace ( 'target: ' + evt.target ) 
        trace ( 'type: ' + evt.type ) 
        trace ( 'data: ' + evt.target.data ) 
}


///
///     IN jgButton.as
///



class jgButton extends MovieClip
{
        private static var EventDispatcherDependancy  =
mx.events.EventDispatcher.initialize( jgButton.prototype );
        private static var jgButtonTypes:Object = {
                onRollOut: 0,
                onRollOver: 0,
                onPress: 0,
                onRelease: 0,
                onReleaseOutside: 0,
                onDragOut: 0,
                onDragOver: 0
                };
        private var addEventListener:Function;
        private var removeEventListener:Function;
        private var dispatchEvent:Function;
        private var dispatchQueue:Function;

        public function jgButton (){}


        ///////////////  STATIC  ///////////////


        public static function initializeMovieClip ( mc:MovieClip ):Void
        {
                // replace MovieClip class with jgButton class
                mc.__proto__ = jgButton.prototype;
        }



        ///////////////  PUBLIC  ///////////////

        ///
        ///             LISTENERS
        ///

        public function showEvents (  )
        {
                if ( arguments.length == 0 )
                {
                        //
                        //      SHOW ALL
                        //
                        for ( var type in jgButtonTypes )
                        {
                                if ( !jgButtonTypes [ type ] )
                                {
                                        jgButtonTypes [ type ] = 1;
                                        this [ type ] = jgButtonEvent ( type
);
                                }
                        }
                }
                else
                {
                        for ( var i in arguments )
                        {
                                var type:String = arguments[ i ];

                                if ( !jgButtonTypes [ type ] )
                                {
                                        jgButtonTypes [ type ] = 1;
                                        this [ type ] = jgButtonEvent ( type
);
                                }
                        }
                }
        }

        public function hideEvents ( )
        {
                if ( arguments.length == 0 )
                {
                        //
                        //      DELETE ALL
                        //
                        for ( var type in jgButtonTypes )
                        {
                                if ( jgButtonTypes [ type ] )
                                {
                                        jgButtonTypes [ type ] = 0;
                                        delete this [ type ];
                                }
                        }
                }
                else
                {
                        for ( var i in arguments )
                        {
                                var type:String = arguments [ i ];

                                if ( jgButtonTypes [ type ] )
                                {
                                        jgButtonTypes [ type] = 0;
                                        delete this [ type ];
                                }
                        }
                }
        }



        ///////////////  PRIVATE  ///////////////


        ///
        ///             EVENT FUNCTION
        ///

        private function jgButtonEvent ( type:String )
        {
                return function()
                {
                        var evt = {};
                        evt.type = type;
                        evt.target = this;
                        this.dispatchEvent ( evt );
                }
        }
}





_____________________________

Jesse Graupmann
www.jessegraupmann.com 
www.justgooddesign.com/blog/ 
_____________________________



-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Muzak
Sent: Saturday, June 02, 2007 5:54 PM
To: [email protected]
Subject: Re: [Flashcoders] AS 2 Delegate question

This isn't about quick and easy, but about encapsulation, OOP and best
practices.
Quick and easy also often means quick and dirty ;-)

Here's a stripped down version of a custom button.

import mx.events.EventDispatcher;
import mx.utils.Delegate;

class com.muzakdeezign.samples.MyButton extends MovieClip {
 // decorate class with EventDispatcher
 private static var dispatcherInit =
EventDispatcher.initialize(com.muzakdeezign.samples.MyButton.prototype);

 // declare EvenDispatcher methods
 public var addEventListener:Function;
 public var removeEventListener:Function;
 public var dispatchEvent:Function;

 private var back_mc:MovieClip;

 private var __index:Number;

 function MyButton() {
  this.init();
 }

 private function init():Void {
  this.back_mc.onRelease = Delegate.create(this, this.backReleaseHandler);
 }

 private function backReleaseHandler():Void {
  this.dispatchEvent({type:"click"});
 }

 private function setIndex():Void {
 }

 function get index():Number {
  return this.__index;
 }
 function set index(val:Number):Void {
  this.__index = val;
  this.setIndex();
 }

}

Here's a screencast making the above:
http://muzakdeezign.com/flashcoders/create_component/MyButton.html

regards,
Muzak


----- Original Message ----- 
From: "Jesse Graupmann" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Sunday, June 03, 2007 1:43 AM
Subject: RE: [Flashcoders] AS 2 Delegate question


> Muzak,
>
> You bring this up every time proxy is mentioned. Do you mind sharing an
> actual code example that is just as quick and easy?
>
> Hasta,
> Jesse


_______________________________________________
[email protected]
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


_______________________________________________
[email protected]
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