6 Replies Latest reply: Jun 1, 2009 7:19 AM by Ziggizag RSS

    AS3: custom events help needed !!!

    Ziggizag Community Member

      I have a loop where I need to download certain number of images. This comes to the problem: each image loading process is a loader class instance. The COMPLETE event is triggered by event dispatcher while image loading is completed and as d.addEventListener(Event.COMPLETE, onComplete) is assigned to all instances we land with a single onComplete event handler for all downloaded images. That is fine, but not in case I need to treat them all differently.

       

      Normally, I would expect something like:

       

      d.addEventListener(Event.COMPLETE, onComplete, arg)

       

       

      what would allow to pass additional parameter to 'onComplete' event handler to let it know how to process given image. But (of course) that would ruin AS3 creator's sense of programmatic purity, so such an easy way is not provided. Having some research I learnt that actually I need to write my own "custome event" class. More - I even found some examples... but all written is such an Aesopian language that I failed to adapt them to my scenario.

       

      Can you gus help me in solving the issue. Please - in some easy, comprehensive manner... step by step. I hope that will be usefull for all not only me.

       

      Rgs,

      Ziggi

        • 1. Re: AS3: custom events help needed !!!
          theErez Community Member

          ziggi,

          what you try to acomplish isn't that sophisticated, so i'll try to make it as simple as possible.

          indeed, in order to solve your problem you need a custom event class.  it should look something like this:

           

          package com.events
          {
              import flash.events.Event;

           

              public class ZiggiCustomEvent extends Event
              {

                    public static const COMPLETE:String = "complete"; // the event type supported by this event class - you can add more of course

           

                    public var customAgrument:* // the argument to pass - cast it to whatever type you need

           

                    public function ZiggiCustomEvent(type:String, arg:*, bubbles:Boolean=false, cancelable:Boolean=false)
                    {
                        super(type, bubbles, cancelable);

                        customAgrument = arg;
                    }

           

                     override public function clone():Event
                     {
                        var event:Nana10PlayerEvents = new ZiggiCustomEvent (this.type, this.customArgument, this.bubbles, this.cancelable);
                        return event;
                     }

             }

          }

           

          when you dispatch this event, it should look like this:

          dispatchEvent(new ZiggiCustomEvent(ZiggiCustomEvent.COMPLETE, 1)); // send the parameter you need

           

          other option is to remove you argument from the constructor's header, and add it 'later', like this:

          var ziggiCustomEvent:ZiggiCustomEvent = new ZiggiCustomEvent(ZiggiCustomEvent.COMPLETE);

          ziggiCustomEvent.customAgrument = 1;

          dispatchEvent(ziggiCustomEvent);

           

          any option you choose, the listener should look like this:

          obj.addEventListener(ZiggiCustomEvent.COMPLETE, onComplete);

           

          private function onComplete(event:ZiggiCustomEvent):void

          {

               trace(event.customArgrument); // retrieve the parameter you sent

          }

           

          hope this makes it clearer

           

          eRez

          • 2. Re: AS3: custom events help needed !!!
            Ziggizag Community Member

            Thank you (the)Erez for your help but I need a little more. I need to understand how exatly pass the argument to event handler.

             

            Please notice, the "normal" image loading process is as follows (timeline script only, no classes):

            //--------------------------------------

            import flash.net.URLLoader;
            import flash.net.URLLoaderDataFormat;
            import flash.net.URLRequest;


            loadImage(someURLaddress);


            function loadImage(url):void{

                 var requesto:URLRequest=new URLRequest();
                 requesto.url=url;
                 var loader:Loader=new Loader();
                 addListeners(loader.contentLoaderInfo);
                 try{
                      loader.load(requesto);
                 }catch(e:Error){
                      loader.unload();
                      removeListeners();
                 }

            }


            function addListeners(d:IEventDispatcher):void{
                 d.addEventListener(Event.COMPLETE, onComplete);
                 d.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandling);
            }


            function onComplete(e:Event):void{

                 var loaderInfo:LoaderInfo = LoaderInfo(e.target);
                 var loader:Loader = loaderInfo.loader;
                 removeListeners();
                 var dob:DisplayObject = loaderInfo.content;
                 loader.unload();
                 processImage(dob);

            }


            function ioErrorHandling(e:IOErrorEvent):void{

                 trace("Error loading image");
                 removeListeners();
            }


            function removeListeners():void{
                removeEventListener(Event.COMPLETE, onComplete);
                removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandling);
            }


            function processImage(img):void{

                 //for instance:

                 var placeholderMovieClip = getChildByName("placeholder");
                  placeholderMovieClip.addChild(img);

            }

            //--------------------------------------

             

            So, as you may see - there is a complete process shown how to initiate image loading and how to send loaded image to processing.

             

            Now, I would really like to see that when the initial command is not:


            loadImage(someURLaddress);

             

            but:

             

            loadImage(someURLaddress, myArgument);

             

            and final function is not:

             

            function processImage(img):void{

            }

             

            but:

             

            function processImage(img, arg):void{

            }

             

            I would really appreciate that!


            Regards,

            Ziggi

            • 3. Re: AS3: custom events help needed !!!
              theErez Community Member

              first - if you want to use a custom event, you'll have to use classes, timeline script only won't do.

              and second - if i were you i'd create a 'ImageLoader' class, with a public function 'loadImage' which expects both url and 'myArgument'', and eventually will dispatch the custom event you prepared with the 'myArgumanet' u sent.  that way, you'll have an instance of the 'ImageLoader' class, which you'll call with a different argument for each image, and add a listener to that instacne which will receieve that argument.

              • 4. Re: AS3: custom events help needed !!!
                Ziggizag Community Member

                Erez(the) - please, of course I know that using classes is necessary for custom events. Sure. But what is tricky moment is actually "dispatching".

                 

                Can you please write here a short example of this LoadImage class, so we can all see how to pass extra argument through?

                 

                Please!

                • 5. Re: AS3: custom events help needed !!!
                  theErez Community Member

                  package com

                  {

                       import flash.events.EventDispatcher

                        import flash.net.URLLoader;
                       import flash.net.URLLoaderDataFormat;
                       import flash.net.URLRequest;

                   

                       public myParameter:*;

                   

                       public class ImageLoader extends EventDispatcher

                       {

                            public function ImageLoader() {}

                   

                            public function loadImage(url:String, arg:*):void{

                                 myParameter = arg;

                                 var requesto:URLRequest=new URLRequest();
                                 requesto.url=url;
                                 var loader:Loader=new Loader();
                                 addListeners(loader.contentLoaderInfo);
                                 try{
                                      loader.load(requesto);
                                 }catch(e:Error){
                                      loader.unload();
                                      removeListeners();
                                 }

                       }


                       private function addListeners(d:IEventDispatcher):void{
                            d.addEventListener(Event.COMPLETE, onComplete);
                            d.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandling);
                       }


                       private function onComplete(e:Event):void{

                            var loaderInfo:LoaderInfo = LoaderInfo(e.target);
                            var loader:Loader = loaderInfo.loader;
                            removeListeners();
                            var dob:DisplayObject = loaderInfo.content;
                            loader.unload();

                            var customEvent:CustomEvent = new CustomEvent(CustromEvent.COMPLETE);

                            customEvent.img = dob;

                            customEvent.customArgument = myParameter;

                            dispatchEvent(customEvent)         
                            //processImage(dob);

                       }


                       privtate function ioErrorHandling(e:IOErrorEvent):void{

                            trace("Error loading image");
                            removeListeners();
                       }


                       private function removeListeners():void{
                           removeEventListener(Event.COMPLETE, onComplete);
                           removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandling);
                       }


                       /*private function processImage(img):void{

                            //for instance:

                            var placeholderMovieClip = getChildByName("placeholder");
                             placeholderMovieClip.addChild(img);

                       }*/

                     }

                  }

                   

                  ********************************

                  var imageLoader:ImageLoader = new ImageLoaer();

                  imageLoader.loadImage(pathToImage, param);

                  imageLoader.addEventListener(CustomEvnet.COMPLETE, processImage);

                  function processImage(event:CustomEvent):void

                  {

                       //for instance:

                        var placeholderMovieClip = getChildByName("placeholder");
                        placeholderMovieClip.addChild(event.img);

                        trace(event.customArgument)

                  }

                  • 6. Re: AS3: custom events help needed !!!
                    Ziggizag Community Member

                    1000 times THANK YOU !!!