The EventDispatcher methods need to be public
public var addEventListener:Function;
public var removeEventListener:Function;
public var dispatchEvent:Function;
And I usually set the events on the Class itself, but use a child movieclip
(which you could see in the screencast).
Someone (not necessarily you) could set events from outside the instance,
messing up your component.
var box = this.attachMovie ( 'box', 'box', this.getNextHighestDepth());
box.onPress = Delegate.create(this, boxPressHandler);
It's no big deal if you will be the only one who will ever use the component.
You'll just have to remember not to use any of the
MovieClip events from outside the instance.
I know it sounds silly, because that's why you created the class in the first
place, but when you distribute alot of components it's
kinda out of your hands ;-)
You'd be surprised how many people drag a Button Component from the components
panel on stage and then go:
myButton.onRelease = function(){}
rather than using addEventListener("click", handler);
regards,
Muzak
----- Original Message -----
From: "Jesse Graupmann" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Sunday, June 03, 2007 5:52 AM
Subject: RE: [Flashcoders] AS 2 Delegate question
> 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 );
> }
> }
> }
>
>
_______________________________________________
[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