Re: [Flashcoders] Event Handling in ActionScript 3
>just can't think of one now. The most common case where you do not extend the EventDispatcher is when you need to extend the Proxy class in order to catch dynamic method and property calls. In that case you need to extend a class that does not extend EventDispatcher itself you can do something like this: public class Test extends Proxy implements IEventDispatcher { private var _eventDispatcher:EventDispatcher; public function Test() { _eventDispatcher = new EventDispatcher(this); }; public function dispatchEvent(event:Event):Boolean { return _eventDispatcher.dispatchEvent(event); }; public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void { return _eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference):void }; ... }; Greetz Erik On 1/29/08, Guybrush Threepwood <[EMAIL PROTECTED]> wrote: > > Thank you again! > I can't think of a reason not to extend EventDispatcher. I think most > classes in AS3 seem to be a subclass of it. > I'm sure there must be cases where you can't just extend EventDispatcher. > I > just can't think of one now. > > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Event Handling in ActionScript 3
Thank you again! I can't think of a reason not to extend EventDispatcher. I think most classes in AS3 seem to be a subclass of it. I'm sure there must be cases where you can't just extend EventDispatcher. I just can't think of one now. Thanks! Guybrush ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Event Handling in ActionScript 3
Hi, basically you look at every method in the interface, and write a method in your own class with the exact same signature and then you declare the class as an implemenation of that interface. Say you'd have an interface ISerializable which would allow you to convert an object to xml , the interface would have a method: public function serialize():XML; And each class that implemented it would have to write the actual method to do so. And about the monkey island thing, I'm not sure myself now and then:) greetz JC On Mon, Jan 28, 2008 at 3:43 AM, Guybrush Threepwood < [EMAIL PROTECTED]> wrote: > Hans/JC/Glen,Thanks for your great advice. > > I understand the new event mechanism is far better than the callback thing > I > used to do. > I think I'll do as you say. Just wanted to know if it was possible at all. > > Any URLs on how to implement IEventDispatcher? I'm not used to implement > interfaces. I'm used to extend classes (using inheritance) but not sure > how > to implement interfaces in AS3. > > Thanks!! > Guybrush > > PS: How do you know I'm not in Monkey Island? > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Event Handling in ActionScript 3
Hans/JC/Glen,Thanks for your great advice. I understand the new event mechanism is far better than the callback thing I used to do. I think I'll do as you say. Just wanted to know if it was possible at all. Any URLs on how to implement IEventDispatcher? I'm not used to implement interfaces. I'm used to extend classes (using inheritance) but not sure how to implement interfaces in AS3. Thanks!! Guybrush PS: How do you know I'm not in Monkey Island? ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Event Handling in ActionScript 3
Hi, You probably want to do something like this: 1. Create a custom event class that extends flash.events.Event and allows you to add & pass variables - see myEvent below 2. In your class that dispatches events, extend flash.events.EventDispatcher (or implement IEventDispatcher interface) - all descendents of DisplayObject get this for free so Sprites & MovieClips can call dispatchEvent without implementing extra code, 3. Use addEventListener in the class that will receive events - you should not need to use Delegate in AS3 - not sure what happens with timeline based code, but in classes, the scope issue is not a problem like AS2. See your code example extended a bit below... HTH Glen import org.guybrush.MySuperFeaturedClass; import org.guybrush.events.myEvent; import flash.events.Event; class MyWrapperClass { private var mySuperObject:MySuperFeaturedClass; function whatever( n:String ):void{ mySuperObject = new MySuperFeaturedClass(); mySuperObject.addEventListener(myEvent.MY_EVENT_1, eventHandler)); mySuperObject.addEventListener(myEvent.MY_EVENT_2, eventHandler)); mySuperObject.addEventListener(myEvent.MY_EVENT_3, eventHandler)); mySuperObject.mDoSomeAsynchronousTask( n ); } private function eventHandler(event:Event):Void { var arg1:String = event.arg1; switch(event.type){ case myEvent.MY_EVENT_1: trace("ok, task returned ...'"); break; case myEvent.MY_EVENT_2: trace("look! task returned ..."); break; case myEvent.MY_EVENT_3: trace("wel... task returned ..."); break; default: trace("oh! task returned " + event.type); break; } } } } package org.guybrush.events { //Simple event class that allows you to extend Event and add variables //& constants to make it easier... import flash.events.Event; public class MyEvent extends Event { public var arg1:String; public static const MY_EVENT_1:String = "eventType1"; public static const MY_EVENT_2:String = "eventType2"; public static const MY_EVENT_3:String = "eventType3"; public function MyEvent(type:String, arg1:String = null) :void { super(type); this.arg1 = arg1; } override public function clone():Event { return new MyEvent(this.type, this.arg1); } } } } package org.guybrush { import org.guybrush.events.myEvent; import flash.events.EventDispatcher; public class MySuperFeaturedClass extends EventDispatcher { public function mDoSomeAsynchronousTask(arg:Object):void { // ... //... //task finished switch(result) { case "one": dispatchEvent(new myEvent(myEvent.MY_EVENT_1, "hello from arg1")); break; case "red": dispatchEvent(new myEvent(myEvent.MY_EVENT_2, "hello from arg1")); break; default: dispatchEvent(new myEvent(myEvent.MY_EVENT_2, "hello from arg1")); break; } } } } Guybrush Threepwood wrote: Hello. I'm new to AS3. I'm using Flash CS3.I've been reading recent posts about event handling, EventDispatcher, and the Delegate class which used in AS2. I've read the documentation about the EventDispatcher class, and learnt how to use it in Flash. How ever I can't seem to fin a way to do what I used to do in AS2. I didn't use Delegate. I did something like this: import org.guybrush.MySuperFeaturedClass; class MyWrapperClass { var mySuperObject:MySuperFeaturedClass; function whatever( n:String ):void{ this.mySuperObject = new MySuperFeaturedClass(); this.mySuperObject.mDoSomeAsynchronousTask( n ); var self:MyWrapperClass = this; *this.mySuperObject.mOnTaskCompleted = function ( type:String, arg2:String ){* switch(type){ case "one": trace("ok, task returned 'one'"); break; case "red": trace("look! task returned 'red'"); break; case "bananas": trace("wel... task returned 'bananas'"); break; default: trace("oh! task returned " + type); break; } }; } } I know how to achieve the same with addEventListener. The thing is, doing it that way I always need to create handling function for each event I'm "registering". And the other disadvantage is that when the class I'm consuming is a custom class coded by my self, I need
Re: [Flashcoders] Event Handling in ActionScript 3
Hi, If you dont like the as3 event mechanism, you can write your own, but looking at your code, there are a number of things I wouldn't do: - setting local variables on the activation stack, it leads to memory waste - executing the asynchronoustask before setting the callback handlers - prepending everything with m - using simple strings as type instead of static public event types - using the callback mechanism (overriding the onTasketc method) combined with the event types and required params So to be honest I wouldnt port this way of working to AS3 , I'd take it as an opportunity to do it better this time around. We're not on monkey island you know;-) greetz JC On Sat, Jan 26, 2008 at 3:48 PM, Guybrush Threepwood < [EMAIL PROTECTED]> wrote: > Hello. I'm new to AS3. I'm using Flash CS3.I've been reading recent posts > about event handling, EventDispatcher, and the Delegate class which used > in > AS2. > I've read the documentation about the EventDispatcher class, and learnt > how > to use it in Flash. > > How ever I can't seem to fin a way to do what I used to do in AS2. I > didn't > use Delegate. I did something like this: > > import org.guybrush.MySuperFeaturedClass; > class MyWrapperClass { >var mySuperObject:MySuperFeaturedClass; >function whatever( n:String ):void{ >this.mySuperObject = new MySuperFeaturedClass(); >this.mySuperObject.mDoSomeAsynchronousTask( n ); >var self:MyWrapperClass = this; >*this.mySuperObject.mOnTaskCompleted = function ( type:String, > arg2:String ){* >switch(type){ >case "one": >trace("ok, task returned 'one'"); >break; >case "red": >trace("look! task returned 'red'"); >break; >case "bananas": >trace("wel... task returned 'bananas'"); >break; >default: >trace("oh! task returned " + type); >break; >} >}; >} > } > > I know how to achieve the same with addEventListener. The thing is, doing > it > that way I always need to create handling function for each event I'm > "registering". And the other disadvantage is that when the class I'm > consuming is a custom class coded by my self, I need to either make it a > subclass of EventDispatcher, or implement its interface somehow. > > So basically, my question is: *"how can I do what I used to to in AS2, now > in AS3?"* > > > Thanks in advance for any replies, > Guybrush > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
[Flashcoders] Event Handling in ActionScript 3
Hello. I'm new to AS3. I'm using Flash CS3.I've been reading recent posts about event handling, EventDispatcher, and the Delegate class which used in AS2. I've read the documentation about the EventDispatcher class, and learnt how to use it in Flash. How ever I can't seem to fin a way to do what I used to do in AS2. I didn't use Delegate. I did something like this: import org.guybrush.MySuperFeaturedClass; class MyWrapperClass { var mySuperObject:MySuperFeaturedClass; function whatever( n:String ):void{ this.mySuperObject = new MySuperFeaturedClass(); this.mySuperObject.mDoSomeAsynchronousTask( n ); var self:MyWrapperClass = this; *this.mySuperObject.mOnTaskCompleted = function ( type:String, arg2:String ){* switch(type){ case "one": trace("ok, task returned 'one'"); break; case "red": trace("look! task returned 'red'"); break; case "bananas": trace("wel... task returned 'bananas'"); break; default: trace("oh! task returned " + type); break; } }; } } I know how to achieve the same with addEventListener. The thing is, doing it that way I always need to create handling function for each event I'm "registering". And the other disadvantage is that when the class I'm consuming is a custom class coded by my self, I need to either make it a subclass of EventDispatcher, or implement its interface somehow. So basically, my question is: *"how can I do what I used to to in AS2, now in AS3?"* Thanks in advance for any replies, Guybrush ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders