4 Replies Latest reply on May 13, 2010 3:25 AM by Vanden_B

    Passing parameters through AddEventListener / Get event target attributes

    Freydaklin Level 1

      As the title says, I'm trying to get parameters passed through the AddEventListener function.

      I know there are many examples about that subject on the net, however, I seem to have a slightly different situation:

      private function set_main_image(img_src:String, width:int, height:int):void
      {
           var target_img:Image = new Image();
           target_img = event.target as Image;
           img_main.source     = target_img.source;
      }
      
      private function loader_complete(event:Event):void
      {
           // Data has been downloaded
           var data:String = event.currentTarget.data;
           var xml:XML = new XML(data);
           
           var data_parsed:String = "";
           
           for each(var img:XML in xml.children())
           {
                var thumb:Image = new Image();
                var thumb_dimensions:Array =
                     get_dimensions(img.child("width"), img.child("height"), 60, 60);
                
                thumb.source          = img.child("filename").toString();
                thumb.width          = thumb_dimensions[0];
                thumb.height          = thumb_dimensions[1];
                thumb.verticalCenter     = "middle";
                thumb.horizontalCenter     = "center";
                
                thumb.addEventListener(MouseEvent.CLICK,
                     function(event:MouseEvent):void
                     {
                          set_main_image(img.child("filename").toString(), img.child("width"), img.child("height"));
                     }, false, 0, false);
                thumbs_container.addElement(thumb);
           }
      }

       

      As you can see, the code has downloaded data and created a number of images to be put into a gallery.

      Now, what I need is a CLICK event on each of those images (those are the thumbnails), so that by clicking a thumbnail, the main image will be changed.

      The example above is based on many found on the web, and compiles without errors. Now, when I run it, it seems to work fine, however, it will always set the main image to the LAST image. I think this is due to how the function is passed as a parameter (a pointer to the function and not a copy of that function), so that, whatever thumbnail I click, it will always consider it as the LAST thumbnail.

       

      Another thing that seemed to be pretty close to what I need is to make the set_main_image() function accept an :Event or :MouseEvent parameter, and use that parameter's target to try to identify the thumbnail's image source, but I haven't got that working so far.

       

      Thank you very much for any help!