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 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
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders



--

Glen Pike
01736 759321
www.glenpike.co.uk <http://www.glenpike.co.uk>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to