RE: [Flashcoders] Need help understanding EventDispatcher
Btw did I mention it also supports event bubbling? -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bjorn Schultheiss Sent: Thursday, 13 April 2006 11:39 PM To: 'Flashcoders mailing list' Subject: RE: [Flashcoders] Need help understanding EventDispatcher class com.qdc.events.QDispatcher { static var $instance:QDispatcher = undefined; private var qDispatcher_listeners:Object; private var qDispatcher_queues:Object; public static var CLASS_NAME : String = "QDispatcher"; static function initialize(p_obj:Object):Void { if ($instance == undefined) { $instance = new QDispatcher; } p_obj.dispatchEvent = $instance.dispatchEvent; p_obj.queueEvent = $instance.queueEvent; p_obj.dispatchQueue = $instance.dispatchQueue; p_obj.eventListenerExists = $instance.eventListenerExists; p_obj.addEventListener = $instance.addEventListener; p_obj.listAllListeners = $instance.listAllListeners; p_obj.removeEventListener = $instance.removeEventListener; p_obj.removeAllEventListeners = $instance.removeAllEventListeners; } // internal function to locate listeners: static function $indexOfListener(p_listeners:Array,p_obj:Object,p_function:String):Number { var l:Number = p_listeners.length; var i:Number = -1; while (++i < l) { var obj:Object = p_listeners[i]; if (obj.o == p_obj && obj.f == p_function) { return i; } } return -1; } static function $dispatchEvent(p_dispatchObj:Object,p_listeners:Array,p_eventObj:Object) { //_global.log.debug( CLASS_NAME+".plisteners.length="+p_listeners.length ); //trace( CLASS_NAME+".plisteners.length="+p_listeners.length ); // Note : Previous gSkinner code copitualted on me( noticed when trying to destroy // DetailsUI it only looped through 3 of 8 elements. //var i:String; // trick from MM: fixes problem with users removing items from listeners while it executes. //for (i in p_listeners) { var len : Number = (p_listeners.length-1); for ( var i : Number = len; i >= 0; i-- ) { var o:Object = p_listeners[i].o; var oType:String = typeof(o); var f:String = p_listeners[i].f; //_global.log.debug( CLASS_NAME+" i="+i+" oType="+oType+" o.handleEvent="+o.handleEvent+" f="+f ); if (oType == "object" || oType == "movieclip") { if (o.handleEvent != undefined && f == undefined) { o.handleEvent(p_eventObj); } else { if (f == undefined) { f = p_eventObj.type; } o[f](p_eventObj); } } else { // function o.apply(p_dispatchObj,[p_eventObj]); } } } /** * Make dispatcher a bubbling dispatcher * Given that dispatcher has a method dispatchEvent(evt), this * method is replaced by a method which additionally dispatches the event * to the parent of dispatcher, unless evt.bubbles === false * @param dispatcher - a MovieClip with a EventDispatcher compatible * dispatchEvent method * @return the changed dispatcher */ public static function initializeBubbling( dispatcher:Object ) : Void { var parentDispatcher = findParentDispatcher( dispatcher); var oldDispatchEvent = dispatcher.dispatchEvent; dispatcher.dispatchEvent = function( evt){ evt.currentTarget = dispatcher; if( evt.eventPhase == undefined){ evt.eventPhase = 2; //at target } else { evt.eventPhase = 3; //bubbling } oldDispatchEvent.call( dispatcher, evt); if( evt.bubbles !== false){ parentDispatcher.dispatchEvent( evt); } } } /** * Return the next parent with a dispatchEvent method or null, if not found * @param dispatcher - a MovieClip with a EventDispatcher compatible * dispatchEvent method * @return - a parent of dispatcher with a EventDispatcher compatible
RE: [Flashcoders] Need help understanding EventDispatcher
uot;.listeners.length="+listeners.length ); for ( var i = 0; imailto:[EMAIL PROTECTED] On Behalf Of jim Sent: Thursday, 13 April 2006 11:32 PM To: 'Flashcoders mailing list' Subject: RE: [Flashcoders] Need help understanding EventDispatcher Ok, that's very nice but if you are going to give an example using it you might want to give out the class as well otherwise the example isn't much help. Jim -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bjorn Schultheiss Sent: 13 April 2006 14:01 To: 'Flashcoders mailing list' Subject: RE: [Flashcoders] Need help understanding EventDispatcher It's an eventDispatcher class based off Grant Skinner's GDispatcher ' http://gskinner.com/blog/'. My class handles queues and dispatching queues nicer :) -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of jim Sent: Thursday, 13 April 2006 9:09 PM To: 'Flashcoders mailing list' Subject: RE: [Flashcoders] Need help understanding EventDispatcher But what is QDispatcher? Do you have a link to it? -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bjorn Schultheiss Sent: 13 April 2006 05:19 To: 'Flashcoders mailing list' Subject: RE: [Flashcoders] Need help understanding EventDispatcher This is better, more extensible // on timeline var myBall:Ball2 = Ball2.createInstance( this, 2 ).init(); myBall.addEventListener( 'ALL', this ); function onBallPress():Void { trace("onBallPress"); } // Ball Class import com.qdc.events.QDispatcher; import com.dynamicflash.utils.Delegate; class Ball2 extends MovieClip { // constants public static var CLASS_NAME : String = "Ball2"; public static var SYMBOL_NAME : String = "testBall"; // methods public function dispatchEvent() {}; public function addEventListener() {}; public static function createInstance( parent:MovieClip, depth:Number, name:String ) : Ball2 { if( depth == undefined) depth = parent.getNextHighestDepth(); if( name == undefined) name = SYMBOL_NAME + depth; return Ball2( parent.attachMovie( SYMBOL_NAME, name, depth)); } public function init() : Ball2 { QDispatcher.initialize( this ); this._x = 100; this._y = 100; this.lineStyle(20, 0xff, 100); this.lineTo(0,.2); return this; } public function onLoad() : Void { aBall.onPress = dispatchEvent( { type : 'onBallPress' } ); } } -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Patrick Matte Sent: Thursday, 13 April 2006 1:12 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] Need help understanding EventDispatcher I dont see exactly why use for the event dispatcher. Wouldn't this be good as well ? // on timeline import Ball2.as; import mx.utils.Delegate; var myBall:Ball2 = new Ball2(); myBall.onBallPress = Delegate.create(this,onBallPress); myBall.addEvents(); function onBallPress():Void { trace("onBallPress"); } // ball class import mx.utils.Delegate; class Ball2 { public var onBallPress:Function; private var aBall:MovieClip; public function Ball2 () { // create a ball MC aBall = _level0.createEmptyMovieClip('testBall',_level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); } public function addEvents():Void{ aBall.onPress = Delegate.create(this,onBallPress); } } ___ 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 ___ 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 ___ 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 ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the a
RE: [Flashcoders] Need help understanding EventDispatcher
Ok, that's very nice but if you are going to give an example using it you might want to give out the class as well otherwise the example isn't much help. Jim -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bjorn Schultheiss Sent: 13 April 2006 14:01 To: 'Flashcoders mailing list' Subject: RE: [Flashcoders] Need help understanding EventDispatcher It's an eventDispatcher class based off Grant Skinner's GDispatcher ' http://gskinner.com/blog/'. My class handles queues and dispatching queues nicer :) -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of jim Sent: Thursday, 13 April 2006 9:09 PM To: 'Flashcoders mailing list' Subject: RE: [Flashcoders] Need help understanding EventDispatcher But what is QDispatcher? Do you have a link to it? -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bjorn Schultheiss Sent: 13 April 2006 05:19 To: 'Flashcoders mailing list' Subject: RE: [Flashcoders] Need help understanding EventDispatcher This is better, more extensible // on timeline var myBall:Ball2 = Ball2.createInstance( this, 2 ).init(); myBall.addEventListener( 'ALL', this ); function onBallPress():Void { trace("onBallPress"); } // Ball Class import com.qdc.events.QDispatcher; import com.dynamicflash.utils.Delegate; class Ball2 extends MovieClip { // constants public static var CLASS_NAME : String = "Ball2"; public static var SYMBOL_NAME : String = "testBall"; // methods public function dispatchEvent() {}; public function addEventListener() {}; public static function createInstance( parent:MovieClip, depth:Number, name:String ) : Ball2 { if( depth == undefined) depth = parent.getNextHighestDepth(); if( name == undefined) name = SYMBOL_NAME + depth; return Ball2( parent.attachMovie( SYMBOL_NAME, name, depth)); } public function init() : Ball2 { QDispatcher.initialize( this ); this._x = 100; this._y = 100; this.lineStyle(20, 0xff, 100); this.lineTo(0,.2); return this; } public function onLoad() : Void { aBall.onPress = dispatchEvent( { type : 'onBallPress' } ); } } -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Patrick Matte Sent: Thursday, 13 April 2006 1:12 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] Need help understanding EventDispatcher I dont see exactly why use for the event dispatcher. Wouldn't this be good as well ? // on timeline import Ball2.as; import mx.utils.Delegate; var myBall:Ball2 = new Ball2(); myBall.onBallPress = Delegate.create(this,onBallPress); myBall.addEvents(); function onBallPress():Void { trace("onBallPress"); } // ball class import mx.utils.Delegate; class Ball2 { public var onBallPress:Function; private var aBall:MovieClip; public function Ball2 () { // create a ball MC aBall = _level0.createEmptyMovieClip('testBall',_level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); } public function addEvents():Void{ aBall.onPress = Delegate.create(this,onBallPress); } } ___ 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 ___ 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 ___ 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 ___ 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 ___ Flashcoders@cha
RE: [Flashcoders] Need help understanding EventDispatcher
It's an eventDispatcher class based off Grant Skinner's GDispatcher ' http://gskinner.com/blog/'. My class handles queues and dispatching queues nicer :) -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of jim Sent: Thursday, 13 April 2006 9:09 PM To: 'Flashcoders mailing list' Subject: RE: [Flashcoders] Need help understanding EventDispatcher But what is QDispatcher? Do you have a link to it? -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bjorn Schultheiss Sent: 13 April 2006 05:19 To: 'Flashcoders mailing list' Subject: RE: [Flashcoders] Need help understanding EventDispatcher This is better, more extensible // on timeline var myBall:Ball2 = Ball2.createInstance( this, 2 ).init(); myBall.addEventListener( 'ALL', this ); function onBallPress():Void { trace("onBallPress"); } // Ball Class import com.qdc.events.QDispatcher; import com.dynamicflash.utils.Delegate; class Ball2 extends MovieClip { // constants public static var CLASS_NAME : String = "Ball2"; public static var SYMBOL_NAME : String = "testBall"; // methods public function dispatchEvent() {}; public function addEventListener() {}; public static function createInstance( parent:MovieClip, depth:Number, name:String ) : Ball2 { if( depth == undefined) depth = parent.getNextHighestDepth(); if( name == undefined) name = SYMBOL_NAME + depth; return Ball2( parent.attachMovie( SYMBOL_NAME, name, depth)); } public function init() : Ball2 { QDispatcher.initialize( this ); this._x = 100; this._y = 100; this.lineStyle(20, 0xff, 100); this.lineTo(0,.2); return this; } public function onLoad() : Void { aBall.onPress = dispatchEvent( { type : 'onBallPress' } ); } } -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Patrick Matte Sent: Thursday, 13 April 2006 1:12 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] Need help understanding EventDispatcher I dont see exactly why use for the event dispatcher. Wouldn't this be good as well ? // on timeline import Ball2.as; import mx.utils.Delegate; var myBall:Ball2 = new Ball2(); myBall.onBallPress = Delegate.create(this,onBallPress); myBall.addEvents(); function onBallPress():Void { trace("onBallPress"); } // ball class import mx.utils.Delegate; class Ball2 { public var onBallPress:Function; private var aBall:MovieClip; public function Ball2 () { // create a ball MC aBall = _level0.createEmptyMovieClip('testBall',_level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); } public function addEvents():Void{ aBall.onPress = Delegate.create(this,onBallPress); } } ___ 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 ___ 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 ___ 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 ___ 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
RE: [Flashcoders] Need help understanding EventDispatcher
But what is QDispatcher? Do you have a link to it? -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bjorn Schultheiss Sent: 13 April 2006 05:19 To: 'Flashcoders mailing list' Subject: RE: [Flashcoders] Need help understanding EventDispatcher This is better, more extensible // on timeline var myBall:Ball2 = Ball2.createInstance( this, 2 ).init(); myBall.addEventListener( 'ALL', this ); function onBallPress():Void { trace("onBallPress"); } // Ball Class import com.qdc.events.QDispatcher; import com.dynamicflash.utils.Delegate; class Ball2 extends MovieClip { // constants public static var CLASS_NAME : String = "Ball2"; public static var SYMBOL_NAME : String = "testBall"; // methods public function dispatchEvent() {}; public function addEventListener() {}; public static function createInstance( parent:MovieClip, depth:Number, name:String ) : Ball2 { if( depth == undefined) depth = parent.getNextHighestDepth(); if( name == undefined) name = SYMBOL_NAME + depth; return Ball2( parent.attachMovie( SYMBOL_NAME, name, depth)); } public function init() : Ball2 { QDispatcher.initialize( this ); this._x = 100; this._y = 100; this.lineStyle(20, 0xff, 100); this.lineTo(0,.2); return this; } public function onLoad() : Void { aBall.onPress = dispatchEvent( { type : 'onBallPress' } ); } } -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Patrick Matte Sent: Thursday, 13 April 2006 1:12 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] Need help understanding EventDispatcher I dont see exactly why use for the event dispatcher. Wouldn't this be good as well ? // on timeline import Ball2.as; import mx.utils.Delegate; var myBall:Ball2 = new Ball2(); myBall.onBallPress = Delegate.create(this,onBallPress); myBall.addEvents(); function onBallPress():Void { trace("onBallPress"); } // ball class import mx.utils.Delegate; class Ball2 { public var onBallPress:Function; private var aBall:MovieClip; public function Ball2 () { // create a ball MC aBall = _level0.createEmptyMovieClip('testBall',_level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); } public function addEvents():Void{ aBall.onPress = Delegate.create(this,onBallPress); } } ___ 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 ___ 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 ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
With event dispatching, multiple objects can register themselves to listen to the event. With the way below, there is no way multiple objects can react to onBallPress as you cannot do delegate chaining in Actionscript. Patrick Matte <[EMAIL PROTECTED]> wrote: I dont see exactly why use for the event dispatcher. Wouldn't this be good as well ? // on timeline import Ball2.as; import mx.utils.Delegate; var myBall:Ball2 = new Ball2(); myBall.onBallPress = Delegate.create(this,onBallPress); myBall.addEvents(); function onBallPress():Void { trace("onBallPress"); } // ball class import mx.utils.Delegate; class Ball2 { public var onBallPress:Function; private var aBall:MovieClip; public function Ball2 () { // create a ball MC aBall = _level0.createEmptyMovieClip('testBall',_level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); } public function addEvents():Void{ aBall.onPress = Delegate.create(this,onBallPress); } } ___ 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 - New Yahoo! Messenger with Voice. Call regular phones from your PC and save big. - New Yahoo! Messenger with Voice. Call regular phones from your PC and save big. ___ 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
RE: [Flashcoders] Need help understanding EventDispatcher
Sorry little mistake // not public function onLoad() : Void { aBall.onPress = dispatchEvent( { type : 'onBallPress' } ); } // but Public function onPress() : Void { dispatchEvent( { type : 'onBallPress' } ); } -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bjorn Schultheiss Sent: Thursday, 13 April 2006 2:19 PM To: 'Flashcoders mailing list' Subject: RE: [Flashcoders] Need help understanding EventDispatcher This is better, more extensible // on timeline var myBall:Ball2 = Ball2.createInstance( this, 2 ).init(); myBall.addEventListener( 'ALL', this ); function onBallPress():Void { trace("onBallPress"); } // Ball Class import com.qdc.events.QDispatcher; import com.dynamicflash.utils.Delegate; class Ball2 extends MovieClip { // constants public static var CLASS_NAME : String = "Ball2"; public static var SYMBOL_NAME : String = "testBall"; // methods public function dispatchEvent() {}; public function addEventListener() {}; public static function createInstance( parent:MovieClip, depth:Number, name:String ) : Ball2 { if( depth == undefined) depth = parent.getNextHighestDepth(); if( name == undefined) name = SYMBOL_NAME + depth; return Ball2( parent.attachMovie( SYMBOL_NAME, name, depth)); } public function init() : Ball2 { QDispatcher.initialize( this ); this._x = 100; this._y = 100; this.lineStyle(20, 0xff, 100); this.lineTo(0,.2); return this; } public function onLoad() : Void { aBall.onPress = dispatchEvent( { type : 'onBallPress' } ); } } -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Patrick Matte Sent: Thursday, 13 April 2006 1:12 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] Need help understanding EventDispatcher I dont see exactly why use for the event dispatcher. Wouldn't this be good as well ? // on timeline import Ball2.as; import mx.utils.Delegate; var myBall:Ball2 = new Ball2(); myBall.onBallPress = Delegate.create(this,onBallPress); myBall.addEvents(); function onBallPress():Void { trace("onBallPress"); } // ball class import mx.utils.Delegate; class Ball2 { public var onBallPress:Function; private var aBall:MovieClip; public function Ball2 () { // create a ball MC aBall = _level0.createEmptyMovieClip('testBall',_level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); } public function addEvents():Void{ aBall.onPress = Delegate.create(this,onBallPress); } } ___ 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 ___ 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 ___ 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
RE: [Flashcoders] Need help understanding EventDispatcher
This is better, more extensible // on timeline var myBall:Ball2 = Ball2.createInstance( this, 2 ).init(); myBall.addEventListener( 'ALL', this ); function onBallPress():Void { trace("onBallPress"); } // Ball Class import com.qdc.events.QDispatcher; import com.dynamicflash.utils.Delegate; class Ball2 extends MovieClip { // constants public static var CLASS_NAME : String = "Ball2"; public static var SYMBOL_NAME : String = "testBall"; // methods public function dispatchEvent() {}; public function addEventListener() {}; public static function createInstance( parent:MovieClip, depth:Number, name:String ) : Ball2 { if( depth == undefined) depth = parent.getNextHighestDepth(); if( name == undefined) name = SYMBOL_NAME + depth; return Ball2( parent.attachMovie( SYMBOL_NAME, name, depth)); } public function init() : Ball2 { QDispatcher.initialize( this ); this._x = 100; this._y = 100; this.lineStyle(20, 0xff, 100); this.lineTo(0,.2); return this; } public function onLoad() : Void { aBall.onPress = dispatchEvent( { type : 'onBallPress' } ); } } -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Patrick Matte Sent: Thursday, 13 April 2006 1:12 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] Need help understanding EventDispatcher I dont see exactly why use for the event dispatcher. Wouldn't this be good as well ? // on timeline import Ball2.as; import mx.utils.Delegate; var myBall:Ball2 = new Ball2(); myBall.onBallPress = Delegate.create(this,onBallPress); myBall.addEvents(); function onBallPress():Void { trace("onBallPress"); } // ball class import mx.utils.Delegate; class Ball2 { public var onBallPress:Function; private var aBall:MovieClip; public function Ball2 () { // create a ball MC aBall = _level0.createEmptyMovieClip('testBall',_level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); } public function addEvents():Void{ aBall.onPress = Delegate.create(this,onBallPress); } } ___ 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 ___ 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
RE: [Flashcoders] Need help understanding EventDispatcher
Hey - no prob! Actually, what the 2 other guys contributed, helped ME out quite a bit too. That's what I love about this list - you can learn so much about so many things - and because there are so many different people here, each person explains things in their own way. You picked the BEST Flash list - hands down - so you are fortunate. It's not always easy to find the best list, when you are searching around for a mailing list to join. Sometimes, hearing the same answer, explained from different people, really helps you understand in a much better way. So I'd like to say Thank You too - as you all helped me solidify my understanding of this topic as well. Take Care everybody! Mike -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Daniel Cardenas Sent: Wednesday, April 12, 2006 9:27 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] Need help understanding EventDispatcher Thanks Mike, That does help. I get scope - although admittedly I should pay a little more attention to it... :-) And I wasn't familiar with the Delegate class - clearly something I need to read about. -dc --- [EMAIL PROTECTED] 917-750-6398 AIM: dcardena --- On Apr 12, 2006, at 9:45 PM, Mike Anderson wrote: > Yes, sorry I should have mentioned that too - (about getting rid of > extending the MovieClip). > > I assumed you were purposely extending the MovieClip, but I should > have helped more and told you to get rid of that portion, and just > make a plain vanilla class file. It would have made all the other > stuff you were putting in there, much more pertinent - since you would > no longer have any functionality inherited from another class, you > would have to build all the Event Broadcasting in manually.. > > For the Delegate question, the reason for that, is if you have a > MovieClip or "whatever it is" deeply nested within the movie, and you > start dispatching Events, sometimes Flash gets confused as to where to > send the events. That's what they refer to as "Scope". > > As you can already see, with the _level0, etc. you can have lots of > levels in a Movie - it keeps things organized, and creates kind of a > stacking order - and it even affects things visually - like something > visually appearing beneath something else, if it resides on a lower > level. > > The Delegate stuff, ensures that the Event you are broadcasting, > actually gets to the Object that's supposed to be receiving it - and > not somewhere else. That was a really great thing, when they came out > with it a couple years ago. I am pretty rusty right now with writing > code, etc. but at the time it really fixed a lot of problems with > scope. > > With AS3, I guess that's all fixed - so you can dispatch events to > your hearts content, and Flash takes care of the rest, without having > to use the Delegate stuff. > > I am just a mediocre coder, so I am sure lots of other people will > help with answering this question for you. > > Hope this help a little bit, and good luck :) > > Mike > > -Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of > Johannes Nel > Sent: Wednesday, April 12, 2006 7:57 PM > To: Flashcoders mailing list > Subject: Re: [Flashcoders] Need help understanding EventDispatcher > > you add and use delegate to solve scoping issues. > > On 4/12/06, Johannes Nel <[EMAIL PROTECTED]> wrote: >> >> ok sorry, you are extending movieclip. i thought for soem reason you >> were not. there is no reason for you to be extending movieclip, so >> don't >> >> once you have gotten rid of the inheritance then it should work. the >> reason the onPress function works is due to the fact that you are >> extending movieclip, and then you assign a different refrence for >> that > function. >> >> anyway i hope you understand what i am tryimg to say >> >> > > > -- > j:pn > http://www.lennel.org > ___ > 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 > ___ > 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 Softwa
Re: [Flashcoders] Need help understanding EventDispatcher
I dont see exactly why use for the event dispatcher. Wouldn't this be good as well ? // on timeline import Ball2.as; import mx.utils.Delegate; var myBall:Ball2 = new Ball2(); myBall.onBallPress = Delegate.create(this,onBallPress); myBall.addEvents(); function onBallPress():Void { trace("onBallPress"); } // ball class import mx.utils.Delegate; class Ball2 { public var onBallPress:Function; private var aBall:MovieClip; public function Ball2 () { // create a ball MC aBall = _level0.createEmptyMovieClip('testBall',_level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); } public function addEvents():Void{ aBall.onPress = Delegate.create(this,onBallPress); } } ___ 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
RE: [Flashcoders] Need help understanding EventDispatcher
Or you could use a derivative of EventDispatcher, either GDispatcher or your own and use; instance.addEventListener( 'ALL', this ); instance.removeAllEventListeners(); instance.queueEvent( { type : 'whateverman' } ); instance.dispatchQueue(); -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Johannes Nel Sent: Thursday, 13 April 2006 12:44 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] Need help understanding EventDispatcher i understand. its not extremly readable either. just pointing out it is possible without using a instance variable. ___ 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 ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
i understand. its not extremly readable either. just pointing out it is possible without using a instance variable. ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
Awesome!! Thank you Thank you Thank you. I need to read more about the Delegate class to understand what's going on here- I have some idea, but not a clear grasp. Thank you so much for steering me in the right direction! --- [EMAIL PROTECTED] 917-750-6398 AIM: dcardena --- On Apr 12, 2006, at 9:46 PM, <[EMAIL PROTECTED]> wrote: Try this : import mx.utils.Delegate; class Ball { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC var aBall:MovieClip = _level0.createEmptyMovieClip( 'testBall', _level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); declareEventMethods(aBall);//BWC - CHanged this to pass the item } // pass the mc in public function declareEventMethods(p_mc:MovieClip):Void { p_mc.onPress = Delegate.create(this,onPress); }4 private function onPress () { dispatchEvent( {type: "press", target: this} ); } } - you were trying to detect an OnPRESS eventon the ball class yet it doesn;t extend Movieclip Hi Johannes - Thank you so much for your suggestions - I really do appreciate the help. I've removed MovieClip extension from the class, but it's still not working - it's still the same as where I started - the "Ball" object renders, but events don't trigger the handler. I've streamlined things slightly - eliminated whitespace mostly, Please - I'm so lost here, this seems so simple. This is where the code is at: - timeline.as - import Ball.as; /* create object */ var myBall:Ball = new Ball(); myBall.declareEventMethods(); /* add listener */ myBall.addEventListener("press", myEventHandler); /* define handler */ function myEventHandler(evt:Object):Void { trace('evt.type: ' + evt.type + ' | evt.target' + evt.target); } - Ball.as - import mx.utils.Delegate; class Ball { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC var aBall:MovieClip = _level0.createEmptyMovieClip( 'testBall', _level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); declareEventMethods(); } public function declareEventMethods():Void { onPress = Delegate.create(this,onPress); }4 private function onPress () { dispatchEvent( {type: "press", target: this} ); } } --- [EMAIL PROTECTED] 917-750-6398 AIM: dcardena --- On Apr 12, 2006, at 8:55 PM, Johannes Nel wrote: ok sorry, you are extending movieclip. i thought for soem reason you were not. there is no reason for you to be extending movieclip, so don't once you have gotten rid of the inheritance then it should work. the reason the onPress function works is due to the fact that you are extending movieclip, and then you assign a different refrence for that function. anyway i hope you understand what i am tryimg to say ___ 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 ___ 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 ___ 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 ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or
Re: [Flashcoders] Need help understanding EventDispatcher
Wow! Thanks Stacey - that's far more than 2 cents worth! I'm reading and absorbing - but will need to spend a little more time with your reply :-) --- [EMAIL PROTECTED] 917-750-6398 AIM: dcardena --- On Apr 12, 2006, at 9:38 PM, <[EMAIL PROTECTED]> wrote: My two cents - EventDispatcher you use as a mixin, which basically means its a class you don't use solely on its own, and its purpose is to dynamically add something to object you mix it into. The members or functions of that mixin class basically get added to your class (to the prototype object - someone feel free to correct me if I am wrong - this is my understanding). EventDispatcher affords you 3 primary functions you use - addEventListener,removeEventListener and dispatchEvent. Mixing in a class is an effective way to get additional functionality without inheriting - as AS doesn't support multiple inheritance. With EventDispatcher you can mix it in- in a couple of ways. You can initialize the class itself to be a broadcaster (EventDispatcher.initialize(this)), you can utilie composition and have an object which will then be the event source ( like adding an object in your class that takes care of all the broadcasting responsibilities) and there's another way I havent' seen implemented too often where you pass the class protoype - in fact the first time I saw this approach was on darron schall's blog (http://www.darronschall.com/weblog/archives/000100.cfm). Delegate is a class that basically delegates/proxies the call. IN the past, to do somethign like load in xml and then call a function in the class, you would often see people do something like: myXML=new XML()); myXML.host=this;// or myXML["host"]=this; myXML.onLoad=function(){ this.host.parseLoadedXML(); } myXML.load("file.xml"); WITH Delegate it takes care of making sure items are called in the scope you designate, taking the scope/object and the function it should fire. myXML=new XML()); myXML.onLoad=Delegate.create(this,parseLoadedXML); myXML.load("file.xml"); Delegate returns you a function. You can retain a reference to that function by going somethign like: var d:Function=Delegate.create(this,onHandleMethod); You often see people use delegates this way as well: buttonComponent.addEventListener("click",Delegate.create(this,onButtonC lick)); This is handy- but if you have to remove that listener at a point in time, you have no reference to it in order to remove it, so clean up becomes hard. I usually create a variable to hold the reference and pass it in: public var delegateItem:Function; delegateItem=Delegate.create(this,onButtonClick); buttonComponent.addEventListener("click",delegateItem); THen when i have to remove: buttonComponent.removeEventListener("click",delegateItem); I guess I am feeling verbose tonight. Anyone, jump in and correct me where I might and possibly be wrong :) ___ 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 ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
Thanks Mike, That does help. I get scope - although admittedly I should pay a little more attention to it... :-) And I wasn't familiar with the Delegate class - clearly something I need to read about. -dc --- [EMAIL PROTECTED] 917-750-6398 AIM: dcardena --- On Apr 12, 2006, at 9:45 PM, Mike Anderson wrote: Yes, sorry I should have mentioned that too - (about getting rid of extending the MovieClip). I assumed you were purposely extending the MovieClip, but I should have helped more and told you to get rid of that portion, and just make a plain vanilla class file. It would have made all the other stuff you were putting in there, much more pertinent - since you would no longer have any functionality inherited from another class, you would have to build all the Event Broadcasting in manually.. For the Delegate question, the reason for that, is if you have a MovieClip or "whatever it is" deeply nested within the movie, and you start dispatching Events, sometimes Flash gets confused as to where to send the events. That's what they refer to as "Scope". As you can already see, with the _level0, etc. you can have lots of levels in a Movie - it keeps things organized, and creates kind of a stacking order - and it even affects things visually - like something visually appearing beneath something else, if it resides on a lower level. The Delegate stuff, ensures that the Event you are broadcasting, actually gets to the Object that's supposed to be receiving it - and not somewhere else. That was a really great thing, when they came out with it a couple years ago. I am pretty rusty right now with writing code, etc. but at the time it really fixed a lot of problems with scope. With AS3, I guess that's all fixed - so you can dispatch events to your hearts content, and Flash takes care of the rest, without having to use the Delegate stuff. I am just a mediocre coder, so I am sure lots of other people will help with answering this question for you. Hope this help a little bit, and good luck :) Mike -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Johannes Nel Sent: Wednesday, April 12, 2006 7:57 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] Need help understanding EventDispatcher you add and use delegate to solve scoping issues. On 4/12/06, Johannes Nel <[EMAIL PROTECTED]> wrote: ok sorry, you are extending movieclip. i thought for soem reason you were not. there is no reason for you to be extending movieclip, so don't once you have gotten rid of the inheritance then it should work. the reason the onPress function works is due to the fact that you are extending movieclip, and then you assign a different refrence for that function. anyway i hope you understand what i am tryimg to say -- j:pn http://www.lennel.org ___ 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 ___ 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 ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
Thanks Johannes! Both your example and Stacey's have made my day. Clearly I need to bone up on Delegate. FWIW - Stacey's example came in 11 bytes under yours - but that was only after I removed an extraneous character that threw an error... So it's a draw. :-) Thank you all for all your help! -Daniel --- [EMAIL PROTECTED] 917-750-6398 AIM: dcardena --- On Apr 12, 2006, at 9:49 PM, Johannes Nel wrote: import Ball.as; /* create object */ var myBall:Ball = new Ball(); myBall.declareEventMethods(); /* add listener */ myBall.addEventListener("press", myEventHandler); /* define handler */ function myEventHandler(evt:Object):Void { trace('evt.type: ' + evt.type + ' | evt.target' + evt.target); } import mx.utils.Delegate; class Ball { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; private var aBall:MovieClip; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC aBall = _level0.createEmptyMovieClip( 'testBall',_level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); declareEventMethods(); } public function declareEventMethods():Void { aBall.onPress = Delegate.create(this,onPress); } private function onPress () { dispatchEvent( {type: "press",target: this} ); } } ___ 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 ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
import Ball.as; /* create object */ var myBall:Ball = new Ball(); myBall.declareEventMethods(); /* add listener */ myBall.addEventListener("press", myEventHandler); /* define handler */ function myEventHandler(evt:Object):Void { trace('evt.type: ' + evt.type + ' | evt.target' + evt.target); } import mx.utils.Delegate; class Ball { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; private var aBall:MovieClip; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC aBall = _level0.createEmptyMovieClip( 'testBall',_level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); declareEventMethods(); } public function declareEventMethods():Void { aBall.onPress = Delegate.create(this,onPress); } private function onPress () { dispatchEvent( {type: "press",target: this} ); } } ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
J - you're totally right - i just avoid relying on the caller bit overall as its not as flexible( for how i code ) :) >>>"You often see people use delegates this way as well: > buttonComponent.addEventListener("click",Delegate.create > (this,onButtonClick)); > > This is handy- but if you have to remove that listener at a point in > time, you have no reference to it in order to remove it, so clean up > becomes hard. I usually create a variable to hold the reference and pass > it in: public var delegateItem:Function;" > > not true. > > you can use arguments.caller to remove the listener when adding it like > that. obviously that is only doable in the handler function. > > On 4/12/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: >> >> My two cents - >> >> EventDispatcher you use as a mixin, which basically means its a class >> you don't use solely on its own, and its purpose is to dynamically add >> something to object you mix it into. The members or functions of that >> mixin class basically get added to your class (to the prototype object >> - someone feel free to correct me if I am wrong - this is my >> understanding). EventDispatcher affords you 3 primary functions you >> use - >> addEventListener,removeEventListener and dispatchEvent. Mixing in a >> class is an effective way to get additional functionality without >> inheriting - as AS doesn't support multiple inheritance. >> >> With EventDispatcher you can mix it in- in a couple of ways. You can >> initialize the class itself to be a broadcaster >> (EventDispatcher.initialize(this)), you can utilie composition and >> have an object which will then be the event source ( like adding an >> object in your class that takes care of all the broadcasting >> responsibilities) and there's another way I havent' seen implemented >> too often where you pass the class protoype - in fact the first time I >> saw this approach was on darron schall's blog >> (http://www.darronschall.com/weblog/archives/000100.cfm). >> >> Delegate is a class that basically delegates/proxies the call. IN the >> past, to do somethign like load in xml and then call a function in the >> class, you would often see people do something like: >> >> myXML=new XML()); >> myXML.host=this;// or myXML["host"]=this; >> myXML.onLoad=function(){ >>this.host.parseLoadedXML(); >> } >> myXML.load("file.xml"); >> >> WITH Delegate it takes care of making sure items are called in the >> scope you designate, taking the scope/object and the function it >> should fire. myXML=new XML()); >> myXML.onLoad=Delegate.create(this,parseLoadedXML); >> myXML.load("file.xml"); >> >> Delegate returns you a function. You can retain a reference to that >> function by going somethign like: >> var d:Function=Delegate.create(this,onHandleMethod); >> >> You often see people use delegates this way as well: >> buttonComponent.addEventListener("click",Delegate.create >> (this,onButtonClick)); >> >> This is handy- but if you have to remove that listener at a point in >> time, you have no reference to it in order to remove it, so clean up >> becomes hard. I usually create a variable to hold the reference and >> pass it in: public var delegateItem:Function; >> >> delegateItem=Delegate.create(this,onButtonClick); >> buttonComponent.addEventListener("click",delegateItem); >> >> >> THen when i have to remove: >> buttonComponent.removeEventListener("click",delegateItem); >> >> >> I guess I am feeling verbose tonight. Anyone, jump in and correct me >> where I might and possibly be wrong :) >> >> >> >> >> >> >> >> >> >> ___ >> 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 >> > > > > -- > j:pn > http://www.lennel.org > ___ > 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 ___ 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
RE: [Flashcoders] Need help understanding EventDispatcher
Yes, sorry I should have mentioned that too - (about getting rid of extending the MovieClip). I assumed you were purposely extending the MovieClip, but I should have helped more and told you to get rid of that portion, and just make a plain vanilla class file. It would have made all the other stuff you were putting in there, much more pertinent - since you would no longer have any functionality inherited from another class, you would have to build all the Event Broadcasting in manually.. For the Delegate question, the reason for that, is if you have a MovieClip or "whatever it is" deeply nested within the movie, and you start dispatching Events, sometimes Flash gets confused as to where to send the events. That's what they refer to as "Scope". As you can already see, with the _level0, etc. you can have lots of levels in a Movie - it keeps things organized, and creates kind of a stacking order - and it even affects things visually - like something visually appearing beneath something else, if it resides on a lower level. The Delegate stuff, ensures that the Event you are broadcasting, actually gets to the Object that's supposed to be receiving it - and not somewhere else. That was a really great thing, when they came out with it a couple years ago. I am pretty rusty right now with writing code, etc. but at the time it really fixed a lot of problems with scope. With AS3, I guess that's all fixed - so you can dispatch events to your hearts content, and Flash takes care of the rest, without having to use the Delegate stuff. I am just a mediocre coder, so I am sure lots of other people will help with answering this question for you. Hope this help a little bit, and good luck :) Mike -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Johannes Nel Sent: Wednesday, April 12, 2006 7:57 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] Need help understanding EventDispatcher you add and use delegate to solve scoping issues. On 4/12/06, Johannes Nel <[EMAIL PROTECTED]> wrote: > > ok sorry, you are extending movieclip. i thought for soem reason you > were not. there is no reason for you to be extending movieclip, so > don't > > once you have gotten rid of the inheritance then it should work. the > reason the onPress function works is due to the fact that you are > extending movieclip, and then you assign a different refrence for that function. > > anyway i hope you understand what i am tryimg to say > > -- j:pn http://www.lennel.org ___ 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 ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
Try this : import mx.utils.Delegate; class Ball { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC var aBall:MovieClip = _level0.createEmptyMovieClip( 'testBall', _level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); declareEventMethods(aBall);//BWC - CHanged this to pass the item } // pass the mc in public function declareEventMethods(p_mc:MovieClip):Void { p_mc.onPress = Delegate.create(this,onPress); }4 private function onPress () { dispatchEvent( {type: "press", target: this} ); } } - you were trying to detect an OnPRESS eventon the ball class yet it doesn;t extend Movieclip > Hi Johannes - > > Thank you so much for your suggestions - I really do appreciate the > help. > > I've removed MovieClip extension from the class, but it's still not > working - it's still the same as where I started - the "Ball" object > renders, but events don't trigger the handler. > > I've streamlined things slightly - eliminated whitespace mostly, Please > - I'm so lost here, this seems so simple. > > This is where the code is at: > > - > timeline.as > - > > import Ball.as; > > /* create object */ > var myBall:Ball = new Ball(); > myBall.declareEventMethods(); > > /* add listener */ > myBall.addEventListener("press", myEventHandler); > > /* define handler */ > function myEventHandler(evt:Object):Void { trace('evt.type: ' + > evt.type + ' | evt.target' + evt.target); } > > - > Ball.as > - > > import mx.utils.Delegate; > class Ball { > >public var addEventListener:Function; >public var removeEventListener:Function; >private var dispatchEvent:Function; > >public function Ball () { > > // initialize EventDispatcher > mx.events.EventDispatcher.initialize(this); > > // create a ball MC > var aBall:MovieClip = _level0.createEmptyMovieClip( 'testBall', > _level0.getNextHighestDepth() ); > > // draw ball > aBall._x = 100; > aBall._y = 100; > aBall.lineStyle(20, 0xff, 100); > aBall.lineTo(0,.2); > declareEventMethods(); >} > >public function declareEventMethods():Void { onPress = > Delegate.create(this,onPress); }4 > >private function onPress () { dispatchEvent( {type: "press", > target: this} ); } > > } > > > > > --- > [EMAIL PROTECTED] > 917-750-6398 > AIM: dcardena > --- > > > > > On Apr 12, 2006, at 8:55 PM, Johannes Nel wrote: > >> ok sorry, you are extending movieclip. i thought for soem reason you >> were >> not. there is no reason for you to be extending movieclip, so don't >> >> once you have gotten rid of the inheritance then it should work. the >> reason >> the onPress function works is due to the fact that you are extending >> movieclip, and then you assign a different refrence for that function. >> >> anyway i hope you understand what i am tryimg to say >> ___ >> 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 >> > > ___ > 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 ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
>>"You often see people use delegates this way as well: buttonComponent.addEventListener("click",Delegate.create (this,onButtonClick)); This is handy- but if you have to remove that listener at a point in time, you have no reference to it in order to remove it, so clean up becomes hard. I usually create a variable to hold the reference and pass it in: public var delegateItem:Function;" not true. you can use arguments.caller to remove the listener when adding it like that. obviously that is only doable in the handler function. On 4/12/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > My two cents - > > EventDispatcher you use as a mixin, which basically means its a class you > don't use solely on its own, and its purpose is to dynamically add > something to object you mix it into. The members or functions of that > mixin class basically get added to your class (to the prototype object - > someone feel free to correct me if I am wrong - this is my understanding). > EventDispatcher affords you 3 primary functions you use - > addEventListener,removeEventListener and dispatchEvent. Mixing in a class > is an effective way to get additional functionality without inheriting - > as AS doesn't support multiple inheritance. > > With EventDispatcher you can mix it in- in a couple of ways. You can > initialize the class itself to be a broadcaster > (EventDispatcher.initialize(this)), you can utilie composition and have an > object which will then be the event source ( like adding an object in > your class that takes care of all the broadcasting responsibilities) and > there's another way I havent' seen implemented too often where you pass > the class protoype - in fact the first time I saw this approach was on > darron schall's blog > (http://www.darronschall.com/weblog/archives/000100.cfm). > > Delegate is a class that basically delegates/proxies the call. IN the > past, to do somethign like load in xml and then call a function in the > class, you would often see people do something like: > > myXML=new XML()); > myXML.host=this;// or myXML["host"]=this; > myXML.onLoad=function(){ >this.host.parseLoadedXML(); > } > myXML.load("file.xml"); > > WITH Delegate it takes care of making sure items are called in the scope > you designate, taking the scope/object and the function it should fire. > myXML=new XML()); > myXML.onLoad=Delegate.create(this,parseLoadedXML); > myXML.load("file.xml"); > > Delegate returns you a function. You can retain a reference to that > function by going somethign like: > var d:Function=Delegate.create(this,onHandleMethod); > > You often see people use delegates this way as well: > buttonComponent.addEventListener("click",Delegate.create > (this,onButtonClick)); > > This is handy- but if you have to remove that listener at a point in time, > you have no reference to it in order to remove it, so clean up becomes > hard. I usually create a variable to hold the reference and pass it in: > public var delegateItem:Function; > > delegateItem=Delegate.create(this,onButtonClick); > buttonComponent.addEventListener("click",delegateItem); > > > THen when i have to remove: > buttonComponent.removeEventListener("click",delegateItem); > > > I guess I am feeling verbose tonight. Anyone, jump in and correct me where > I might and possibly be wrong :) > > > > > > > > > > ___ > 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 > -- j:pn http://www.lennel.org ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
My two cents - EventDispatcher you use as a mixin, which basically means its a class you don't use solely on its own, and its purpose is to dynamically add something to object you mix it into. The members or functions of that mixin class basically get added to your class (to the prototype object - someone feel free to correct me if I am wrong - this is my understanding). EventDispatcher affords you 3 primary functions you use - addEventListener,removeEventListener and dispatchEvent. Mixing in a class is an effective way to get additional functionality without inheriting - as AS doesn't support multiple inheritance. With EventDispatcher you can mix it in- in a couple of ways. You can initialize the class itself to be a broadcaster (EventDispatcher.initialize(this)), you can utilie composition and have an object which will then be the event source ( like adding an object in your class that takes care of all the broadcasting responsibilities) and there's another way I havent' seen implemented too often where you pass the class protoype - in fact the first time I saw this approach was on darron schall's blog (http://www.darronschall.com/weblog/archives/000100.cfm). Delegate is a class that basically delegates/proxies the call. IN the past, to do somethign like load in xml and then call a function in the class, you would often see people do something like: myXML=new XML()); myXML.host=this;// or myXML["host"]=this; myXML.onLoad=function(){ this.host.parseLoadedXML(); } myXML.load("file.xml"); WITH Delegate it takes care of making sure items are called in the scope you designate, taking the scope/object and the function it should fire. myXML=new XML()); myXML.onLoad=Delegate.create(this,parseLoadedXML); myXML.load("file.xml"); Delegate returns you a function. You can retain a reference to that function by going somethign like: var d:Function=Delegate.create(this,onHandleMethod); You often see people use delegates this way as well: buttonComponent.addEventListener("click",Delegate.create(this,onButtonClick)); This is handy- but if you have to remove that listener at a point in time, you have no reference to it in order to remove it, so clean up becomes hard. I usually create a variable to hold the reference and pass it in: public var delegateItem:Function; delegateItem=Delegate.create(this,onButtonClick); buttonComponent.addEventListener("click",delegateItem); THen when i have to remove: buttonComponent.removeEventListener("click",delegateItem); I guess I am feeling verbose tonight. Anyone, jump in and correct me where I might and possibly be wrong :) ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
Hi Johannes - Thank you so much for your suggestions - I really do appreciate the help. I've removed MovieClip extension from the class, but it's still not working - it's still the same as where I started - the "Ball" object renders, but events don't trigger the handler. I've streamlined things slightly - eliminated whitespace mostly, Please - I'm so lost here, this seems so simple. This is where the code is at: - timeline.as - import Ball.as; /* create object */ var myBall:Ball = new Ball(); myBall.declareEventMethods(); /* add listener */ myBall.addEventListener("press", myEventHandler); /* define handler */ function myEventHandler(evt:Object):Void { trace('evt.type: ' + evt.type + ' | evt.target' + evt.target); } - Ball.as - import mx.utils.Delegate; class Ball { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC var aBall:MovieClip = _level0.createEmptyMovieClip( 'testBall', _level0.getNextHighestDepth() ); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); declareEventMethods(); } public function declareEventMethods():Void { onPress = Delegate.create(this,onPress); }4 private function onPress () { dispatchEvent( {type: "press", target: this} ); } } --- [EMAIL PROTECTED] 917-750-6398 AIM: dcardena --- On Apr 12, 2006, at 8:55 PM, Johannes Nel wrote: ok sorry, you are extending movieclip. i thought for soem reason you were not. there is no reason for you to be extending movieclip, so don't once you have gotten rid of the inheritance then it should work. the reason the onPress function works is due to the fact that you are extending movieclip, and then you assign a different refrence for that function. anyway i hope you understand what i am tryimg to say ___ 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 ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
you add and use delegate to solve scoping issues. On 4/12/06, Johannes Nel <[EMAIL PROTECTED]> wrote: > > ok sorry, you are extending movieclip. i thought for soem reason you were > not. there is no reason for you to be extending movieclip, so don't > > once you have gotten rid of the inheritance then it should work. the > reason the onPress function works is due to the fact that you are extending > movieclip, and then you assign a different refrence for that function. > > anyway i hope you understand what i am tryimg to say > > -- j:pn http://www.lennel.org ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
ok sorry, you are extending movieclip. i thought for soem reason you were not. there is no reason for you to be extending movieclip, so don't once you have gotten rid of the inheritance then it should work. the reason the onPress function works is due to the fact that you are extending movieclip, and then you assign a different refrence for that function. anyway i hope you understand what i am tryimg to say ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
PS - I'm afraid to ask - why do I need to add in the delegate class? --- [EMAIL PROTECTED] 917-750-6398 AIM: dcardena --- On Apr 12, 2006, at 8:32 PM, Johannes Nel wrote: hopefully this does not spark a big debate. always try to favour composition over inheritance, not that extending movieclip is a bad sollution, however then you start dealing with linkageID's in the ide and all that schlep. anyway that said this is how to fix the code. import mx.utils.Delegate; class Ball extends MovieClip { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC var aBall:MovieClip = _level0.createEmptyMovieClip('testBall', getNextHighestDepth()); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); declareEventMethods(); } public function declareEventMethods():Void { aBall.onMouseDown = Delegate.create(this,onMouseDown); aBall.onMouseUp = Delegate.create(this,onMouseUp ); aBall.onPress = Delegate.create(this,onPress ); aBall.onRelease= Delegate.create(this,onRelease); } private function onMouseDown() { dispatchEvent( {type: "mouseDown", target: this} ); } private function onMouseUp () { dispatchEvent( {type: "mouseUp", target: this} ); } private function onPress () { dispatchEvent( {type: "press", target: this} ); } private function onRelease() { dispatchEvent( {type: "release", target: this} ); } } On 4/12/06, Mike Anderson <[EMAIL PROTECTED]> wrote: If you are extending a MovieClip - doesn't the MovieClip already have all those things "you are trying to recreate" at your disposal? That is the whole point of extending a MovieClip - so you DON'T have to go through all the stuff you are going through right now. I am no guru at this stuff, but I write Class Files on a daily basis, and I NEVER have to deal with stuff like this. I create custom listeners, and dispatch events on a regular basis - and it works just beautifully. It just seems like you are reinventing the wheel, and defeating the whole purpose of why things were created this way in the first place. This is just my initial reaction to what I've seen, and I am sure some more experienced people will chime in on this thread. Mike -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Daniel Cardenas Sent: Wednesday, April 12, 2006 7:10 PM To: flashcoders@chattyfig.figleaf.com Subject: [Flashcoders] Need help understanding EventDispatcher Hi, I'm trying to learn event handling using the EventDispatcher class and am having a hard time getting a test to work. I'm also fairly new to OOP, so I'm having a hard time finding the problem in my code. I've created a simple "Ball" class that creates an empty MC and then draws a circle in it. I've defined some event methods in the class and contained them within a "declareEventMethods" method. I don't know if that's the right way to do it or not - I modeled this test on various tutorials I've found online, but it's not working. What I'm trying to do is create a new instance of the "Ball" class called "myBall", then notify an event handler of events dispatched by myBall. The handler should then execute a trace command. The "Ball" class assigns the name "testBall" to the MC it creates, and I can trigger the event handler if I define a method directly on the MC that's created, ie: testBall.onPress = function () {myEventHandler(testBall);} but that's not what I'm trying to accomplish. I've pasted the code below - any help would be greatly appreciated. Thanks, Daniel Cardenas -- Ball Class -- class Ball extends MovieClip { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC var aBall:MovieClip = _level0.createEmptyMovieClip('testBall', getNextHighestDepth()); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); } public function
Re: [Flashcoders] Need help understanding EventDispatcher
Thanks Johannes, I tried your code but it didn't work - no errors or anything, just didn't trigger the handler. Is there be a problem with my timeline code? Note - I removed some events and methods to simplify the code. Class Code: import mx.utils.Delegate; class Ball extends MovieClip { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC var aBall:MovieClip = _level0.createEmptyMovieClip('testBall', getNextHighestDepth()); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); declareEventMethods(); } public function declareEventMethods():Void { // removed onMouseDown, onMouseUp & onRelease functions to simplify aBall.onPress = Delegate.create(this,onPress ); } // removed onMouseDown, onMouseUp & onRelease functions to simplify private function onPress () { dispatchEvent( {type: "press", target: this} ); } } Timeline Code: /* create object --*/ var myBall:Ball = new Ball(); myBall.declareEventMethods(); /* add listener --*/ // removed onMouseDown, onMouseUp & onRelease events to simplify myBall.addEventListener("press", myEventHandler); /* define handler --*/ function myEventHandler(evt:Object):Void { trace('A ' + evt.type + ' event was dispatched by ' + evt.target); } // this works - but it's not what I'm trying to do... // testBall.onPress = function () {myEventHandler(testBall);} --- [EMAIL PROTECTED] 917-750-6398 AIM: dcardena --- On Apr 12, 2006, at 8:32 PM, Johannes Nel wrote: import mx.utils.Delegate; class Ball extends MovieClip { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC var aBall:MovieClip = _level0.createEmptyMovieClip('testBall', getNextHighestDepth()); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); declareEventMethods(); } public function declareEventMethods():Void { aBall.onMouseDown = Delegate.create(this,onMouseDown); aBall.onMouseUp = Delegate.create(this,onMouseUp ); aBall.onPress = Delegate.create(this,onPress ); aBall.onRelease= Delegate.create(this,onRelease); } private function onMouseDown() { dispatchEvent( {type: "mouseDown", target: this} ); } private function onMouseUp () { dispatchEvent( {type: "mouseUp", target: this} ); } private function onPress () { dispatchEvent( {type: "press", target: this} ); } private function onRelease() { dispatchEvent( {type: "release", target: this} ); } } ___ 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
Re: [Flashcoders] Need help understanding EventDispatcher
and as to why. it was a scope issue, you weren't dispatching the event out of your class, but out of the movieclip. On 4/12/06, Daniel Cardenas <[EMAIL PROTECTED]> wrote: > > Hi Mike, > > Thanks for your reply. > > The class I created was based on a tutorial I found online > (communitymx.com) a subscription-based site that I started a trial > subscription to, but wasn't getting any response to my questions, so > didn't see the point in paying money for. > > Maybe extending MovieClip isn't ideal for this test - I don't know. I'm > just trying to wrap my head around event handling using the > EventDispatcher class. > > > --- > [EMAIL PROTECTED] > 917-750-6398 > AIM: dcardena > --- > > > > > On Apr 12, 2006, at 8:22 PM, Mike Anderson wrote: > > > If you are extending a MovieClip - doesn't the MovieClip already have > > all those things "you are trying to recreate" at your disposal? > > > > That is the whole point of extending a MovieClip - so you DON'T have to > > go through all the stuff you are going through right now. > > > > I am no guru at this stuff, but I write Class Files on a daily basis, > > and I NEVER have to deal with stuff like this. I create custom > > listeners, and dispatch events on a regular basis - and it works just > > beautifully. > > > > It just seems like you are reinventing the wheel, and defeating the > > whole purpose of why things were created this way in the first place. > > > > This is just my initial reaction to what I've seen, and I am sure some > > more experienced people will chime in on this thread. > > > > Mike > > > > -Original Message- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] On Behalf Of Daniel > > Cardenas > > Sent: Wednesday, April 12, 2006 7:10 PM > > To: flashcoders@chattyfig.figleaf.com > > Subject: [Flashcoders] Need help understanding EventDispatcher > > > > > > Hi, > > > > I'm trying to learn event handling using the EventDispatcher class and > > am having a hard time getting a test to work. I'm also fairly new to > > OOP, so I'm having a hard time finding the problem in my code. > > > > I've created a simple "Ball" class that creates an empty MC and then > > draws a circle in it. I've defined some event methods in the class and > > contained them within a "declareEventMethods" method. I don't know if > > that's the right way to do it or not - I modeled this test on various > > tutorials I've found online, but it's not working. > > > > What I'm trying to do is create a new instance of the "Ball" class > > called "myBall", then notify an event handler of events dispatched by > > myBall. The handler should then execute a trace command. > > > > The "Ball" class assigns the name "testBall" to the MC it creates, and > > I > > can trigger the event handler if I define a method directly on the MC > > that's created, ie: > >testBall.onPress = function () {myEventHandler(testBall);} > > > > but that's not what I'm trying to accomplish. > > > > I've pasted the code below - any help would be greatly appreciated. > > > > Thanks, > > > > Daniel Cardenas > > > > > > -- > > Ball Class > > -- > > > > class Ball extends MovieClip { > > > >public var addEventListener:Function; > >public var removeEventListener:Function; > >private var dispatchEvent:Function; > > > >public function Ball () { > > // initialize EventDispatcher > > mx.events.EventDispatcher.initialize(this); > > > > // create a ball MC > > var aBall:MovieClip = _level0.createEmptyMovieClip('testBall', > > getNextHighestDepth()); > > > > // draw ball > > aBall._x = 100; > > aBall._y = 100; > > aBall.lineStyle(20, 0xff, 100); > > aBall.lineTo(0,.2); > >} > > > >public function declareEventMethods():Void { > > onMouseDown = function() { > > dispatchEvent( {type: "mouseDown", target: this} ); > > } > > onMouseUp = function() { >
Re: [Flashcoders] Need help understanding EventDispatcher
Hi Mike, Thanks for your reply. The class I created was based on a tutorial I found online (communitymx.com) a subscription-based site that I started a trial subscription to, but wasn't getting any response to my questions, so didn't see the point in paying money for. Maybe extending MovieClip isn't ideal for this test - I don't know. I'm just trying to wrap my head around event handling using the EventDispatcher class. --- [EMAIL PROTECTED] 917-750-6398 AIM: dcardena --- On Apr 12, 2006, at 8:22 PM, Mike Anderson wrote: If you are extending a MovieClip - doesn't the MovieClip already have all those things "you are trying to recreate" at your disposal? That is the whole point of extending a MovieClip - so you DON'T have to go through all the stuff you are going through right now. I am no guru at this stuff, but I write Class Files on a daily basis, and I NEVER have to deal with stuff like this. I create custom listeners, and dispatch events on a regular basis - and it works just beautifully. It just seems like you are reinventing the wheel, and defeating the whole purpose of why things were created this way in the first place. This is just my initial reaction to what I've seen, and I am sure some more experienced people will chime in on this thread. Mike -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Daniel Cardenas Sent: Wednesday, April 12, 2006 7:10 PM To: flashcoders@chattyfig.figleaf.com Subject: [Flashcoders] Need help understanding EventDispatcher Hi, I'm trying to learn event handling using the EventDispatcher class and am having a hard time getting a test to work. I'm also fairly new to OOP, so I'm having a hard time finding the problem in my code. I've created a simple "Ball" class that creates an empty MC and then draws a circle in it. I've defined some event methods in the class and contained them within a "declareEventMethods" method. I don't know if that's the right way to do it or not - I modeled this test on various tutorials I've found online, but it's not working. What I'm trying to do is create a new instance of the "Ball" class called "myBall", then notify an event handler of events dispatched by myBall. The handler should then execute a trace command. The "Ball" class assigns the name "testBall" to the MC it creates, and I can trigger the event handler if I define a method directly on the MC that's created, ie: testBall.onPress = function () {myEventHandler(testBall);} but that's not what I'm trying to accomplish. I've pasted the code below - any help would be greatly appreciated. Thanks, Daniel Cardenas -- Ball Class -- class Ball extends MovieClip { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC var aBall:MovieClip = _level0.createEmptyMovieClip('testBall', getNextHighestDepth()); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); } public function declareEventMethods():Void { onMouseDown = function() { dispatchEvent( {type: "mouseDown", target: this} ); } onMouseUp = function() { dispatchEvent( {type: "mouseUp", target: this} ); } onPress = function() { dispatchEvent( {type: "press", target: this} ); } onRelease = function() { dispatchEvent( {type: "release", target: this} ); } } } -- Timeline Code -- /* create object --*/ var myBall:Ball = new Ball(); myBall.declareEventMethods(); /* add listener --*/ myBall.addEventListener("mouseDown", myEventHandler); myBall.addEventListener("mouseUp", myEventHandler); myBall.addEventListener("press", myEventHandler); myBall.addEventListener("release", myEventHandler); /* define handler --*/ function myEventHandler(evt:Object):Void { trace('A ' + evt.type + ' event was dispatched by ' + evt.target); } // this works - but it's not what I'm t
Re: [Flashcoders] Need help understanding EventDispatcher
hopefully this does not spark a big debate. always try to favour composition over inheritance, not that extending movieclip is a bad sollution, however then you start dealing with linkageID's in the ide and all that schlep. anyway that said this is how to fix the code. import mx.utils.Delegate; class Ball extends MovieClip { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC var aBall:MovieClip = _level0.createEmptyMovieClip('testBall', getNextHighestDepth()); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); declareEventMethods(); } public function declareEventMethods():Void { aBall.onMouseDown = Delegate.create(this,onMouseDown); aBall.onMouseUp = Delegate.create(this,onMouseUp ); aBall.onPress = Delegate.create(this,onPress ); aBall.onRelease= Delegate.create(this,onRelease); } private function onMouseDown() { dispatchEvent( {type: "mouseDown", target: this} ); } private function onMouseUp () { dispatchEvent( {type: "mouseUp", target: this} ); } private function onPress () { dispatchEvent( {type: "press", target: this} ); } private function onRelease() { dispatchEvent( {type: "release", target: this} ); } } On 4/12/06, Mike Anderson <[EMAIL PROTECTED]> wrote: > > If you are extending a MovieClip - doesn't the MovieClip already have > all those things "you are trying to recreate" at your disposal? > > That is the whole point of extending a MovieClip - so you DON'T have to > go through all the stuff you are going through right now. > > I am no guru at this stuff, but I write Class Files on a daily basis, > and I NEVER have to deal with stuff like this. I create custom > listeners, and dispatch events on a regular basis - and it works just > beautifully. > > It just seems like you are reinventing the wheel, and defeating the > whole purpose of why things were created this way in the first place. > > This is just my initial reaction to what I've seen, and I am sure some > more experienced people will chime in on this thread. > > Mike > > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Daniel > Cardenas > Sent: Wednesday, April 12, 2006 7:10 PM > To: flashcoders@chattyfig.figleaf.com > Subject: [Flashcoders] Need help understanding EventDispatcher > > > Hi, > > I'm trying to learn event handling using the EventDispatcher class and > am having a hard time getting a test to work. I'm also fairly new to > OOP, so I'm having a hard time finding the problem in my code. > > I've created a simple "Ball" class that creates an empty MC and then > draws a circle in it. I've defined some event methods in the class and > contained them within a "declareEventMethods" method. I don't know if > that's the right way to do it or not - I modeled this test on various > tutorials I've found online, but it's not working. > > What I'm trying to do is create a new instance of the "Ball" class > called "myBall", then notify an event handler of events dispatched by > myBall. The handler should then execute a trace command. > > The "Ball" class assigns the name "testBall" to the MC it creates, and I > can trigger the event handler if I define a method directly on the MC > that's created, ie: >testBall.onPress = function () {myEventHandler(testBall);} > > but that's not what I'm trying to accomplish. > > I've pasted the code below - any help would be greatly appreciated. > > Thanks, > > Daniel Cardenas > > > -- > Ball Class > -- > > class Ball extends MovieClip { > >public var addEventListener:Function; >public var removeEventListener:Function; >private var dispatchEvent:Function; > >public function Ball () { > // initialize EventDispatcher > mx.events.EventDispatcher.initialize(this); > > // create a ball MC > var aBall:MovieClip = _level0.createEmptyMovieClip('testBall', > getNextHighestDepth()); > > // draw ball > aBall._x = 100; > aBall._y = 100; > aB
RE: [Flashcoders] Need help understanding EventDispatcher
If you are extending a MovieClip - doesn't the MovieClip already have all those things "you are trying to recreate" at your disposal? That is the whole point of extending a MovieClip - so you DON'T have to go through all the stuff you are going through right now. I am no guru at this stuff, but I write Class Files on a daily basis, and I NEVER have to deal with stuff like this. I create custom listeners, and dispatch events on a regular basis - and it works just beautifully. It just seems like you are reinventing the wheel, and defeating the whole purpose of why things were created this way in the first place. This is just my initial reaction to what I've seen, and I am sure some more experienced people will chime in on this thread. Mike -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Daniel Cardenas Sent: Wednesday, April 12, 2006 7:10 PM To: flashcoders@chattyfig.figleaf.com Subject: [Flashcoders] Need help understanding EventDispatcher Hi, I'm trying to learn event handling using the EventDispatcher class and am having a hard time getting a test to work. I'm also fairly new to OOP, so I'm having a hard time finding the problem in my code. I've created a simple "Ball" class that creates an empty MC and then draws a circle in it. I've defined some event methods in the class and contained them within a "declareEventMethods" method. I don't know if that's the right way to do it or not - I modeled this test on various tutorials I've found online, but it's not working. What I'm trying to do is create a new instance of the "Ball" class called "myBall", then notify an event handler of events dispatched by myBall. The handler should then execute a trace command. The "Ball" class assigns the name "testBall" to the MC it creates, and I can trigger the event handler if I define a method directly on the MC that's created, ie: testBall.onPress = function () {myEventHandler(testBall);} but that's not what I'm trying to accomplish. I've pasted the code below - any help would be greatly appreciated. Thanks, Daniel Cardenas -- Ball Class -- class Ball extends MovieClip { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC var aBall:MovieClip = _level0.createEmptyMovieClip('testBall', getNextHighestDepth()); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); } public function declareEventMethods():Void { onMouseDown = function() { dispatchEvent( {type: "mouseDown", target: this} ); } onMouseUp = function() { dispatchEvent( {type: "mouseUp", target: this} ); } onPress = function() { dispatchEvent( {type: "press", target: this} ); } onRelease = function() { dispatchEvent( {type: "release", target: this} ); } } } -- Timeline Code -- /* create object --*/ var myBall:Ball = new Ball(); myBall.declareEventMethods(); /* add listener --*/ myBall.addEventListener("mouseDown", myEventHandler); myBall.addEventListener("mouseUp", myEventHandler); myBall.addEventListener("press", myEventHandler); myBall.addEventListener("release", myEventHandler); /* define handler --*/ function myEventHandler(evt:Object):Void { trace('A ' + evt.type + ' event was dispatched by ' + evt.target); } // this works - but it's not what I'm trying to do... // testBall.onPress = function () {myEventHandler(testBall);} --- [EMAIL PROTECTED] 917-750-6398 AIM: dcardena --- ___ 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 ___ 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
[Flashcoders] Need help understanding EventDispatcher
Hi, I'm trying to learn event handling using the EventDispatcher class and am having a hard time getting a test to work. I'm also fairly new to OOP, so I'm having a hard time finding the problem in my code. I've created a simple "Ball" class that creates an empty MC and then draws a circle in it. I've defined some event methods in the class and contained them within a "declareEventMethods" method. I don't know if that's the right way to do it or not - I modeled this test on various tutorials I've found online, but it's not working. What I'm trying to do is create a new instance of the "Ball" class called "myBall", then notify an event handler of events dispatched by myBall. The handler should then execute a trace command. The "Ball" class assigns the name "testBall" to the MC it creates, and I can trigger the event handler if I define a method directly on the MC that's created, ie: testBall.onPress = function () {myEventHandler(testBall);} but that's not what I'm trying to accomplish. I've pasted the code below - any help would be greatly appreciated. Thanks, Daniel Cardenas -- Ball Class -- class Ball extends MovieClip { public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; public function Ball () { // initialize EventDispatcher mx.events.EventDispatcher.initialize(this); // create a ball MC var aBall:MovieClip = _level0.createEmptyMovieClip('testBall', getNextHighestDepth()); // draw ball aBall._x = 100; aBall._y = 100; aBall.lineStyle(20, 0xff, 100); aBall.lineTo(0,.2); } public function declareEventMethods():Void { onMouseDown = function() { dispatchEvent( {type: "mouseDown", target: this} ); } onMouseUp = function() { dispatchEvent( {type: "mouseUp", target: this} ); } onPress = function() { dispatchEvent( {type: "press", target: this} ); } onRelease = function() { dispatchEvent( {type: "release", target: this} ); } } } -- Timeline Code -- /* create object --*/ var myBall:Ball = new Ball(); myBall.declareEventMethods(); /* add listener --*/ myBall.addEventListener("mouseDown", myEventHandler); myBall.addEventListener("mouseUp", myEventHandler); myBall.addEventListener("press", myEventHandler); myBall.addEventListener("release", myEventHandler); /* define handler --*/ function myEventHandler(evt:Object):Void { trace('A ' + evt.type + ' event was dispatched by ' + evt.target); } // this works - but it's not what I'm trying to do... // testBall.onPress = function () {myEventHandler(testBall);} --- [EMAIL PROTECTED] 917-750-6398 AIM: dcardena --- ___ 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