1 Reply Latest reply on Aug 10, 2010 4:40 AM by Ned Murphy

    startDrag using Generic method in Actionscript 3

    viji84 Level 1

       

      Hi,

       

      AS3:

      ------

      How to get general id (or) name from xml loaded images. bcoz, i want to drag and drop that images in generic method. Now, i currently used the event.target.name for each one.

       

      So, code length was too large. See my code below.

       

      /*********loading images through xml********/

       

       


      var bg_container_mc:MovieClip

      var bg_bg_myXMLLoader:URLLoader = new URLLoader();

      bg_bg_myXMLLoader.load(new URLRequest("xml/backgroundimages.xml"));

      bg_bg_myXMLLoader.addEventListener(Event.COMPLETE, processXML_bg);

       

      function processXML_bg(e:Event):void {

                var bg_myXML:XML=new XML(e.target.data);

       

                columns_bg=bg_myXML.@COLUMNS;

                bg_my_x=bg_myXML.@XPOSITION;

                bg_my_y=bg_myXML.@YPOSITION;

                bg_my_thumb_width=bg_myXML.@WIDTH;

                bg_my_thumb_height=bg_myXML.@HEIGHT;

                bg_my_images=bg_myXML.IMAGE;

                bg_my_total=bg_my_images.length();

       

                bg_container_mc = new MovieClip();

                bg_container_mc.x=bg_my_x;

                bg_container_mc.y=bg_my_y;

                ContentHead.addChild(bg_container_mc);

       

               

                for (var i:Number = 0; i < bg_my_total; i++) {

       

                         var bg_thumb_url=bg_my_images[i].@THUMB;

       

                          var bg_thumb_loader = new Loader();

                         bg_thumb_loader.load(new URLRequest(bg_thumb_url));

                         bg_thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded_bg);

       

       

                         bg_thumb_loader.name="bg_load"+i;

                         bg_thumb_loader.addEventListener(MouseEvent.MOUSE_DOWN, bg_down)

                         bg_thumb_loader.x = (bg_my_thumb_width-18)*bg_x_counter;

                         bg_thumb_loader.y = (bg_my_thumb_height-45)*bg_y_counter;

       

       

       

                         if (bg_x_counter+1<columns_bg) {

                                   bg_x_counter++;

       

                         } else {

                                   bg_x_counter=0;

                                   bg_y_counter++;

       

                         }

       

                }

      }

      function thumbLoaded_bg(e:Event):void {

                var my_thumb_bg:Loader=Loader(e.target.loader);

               var sprite:Sprite = new Sprite();

                var shape:Shape = new Shape();

       

                shape.graphics.lineStyle(1, 0x0098FF);

                shape.graphics.drawRect(e.target.loader.x-2, e.target.loader.y-2, e.target.loader.width+4, e.target.loader.height+4);

                sprite.addChild(shape);

                sprite.addChild(my_thumb_bg);

                sprite.x=4;

       

                bg_container_mc.addChild(sprite);

       

               

       

                my_thumb_bg.contentLoaderInfo.removeEventListener(Event.COMPLETE, thumbLoaded_bg);

      }

       

       

      //  get name for each image. 

       

       

      I want to change this code in generic method. do needful.

       

       

       

       

      function bg_down(event:MouseEvent):void {

                var bg_name:String=new String(event.currentTarget.name);

               

                var bg_load:MovieClip=event.currentTarget as MovieClip;

       

                if (event.currentTarget.name=="bg_load0") {

       

                        var alaska_mc:alaska_png=new alaska_png();

                         addChild(alaska_mc);

                         alaska_mc.x=stage.mouseX;

                         alaska_mc.y=stage.mouseY;

                         alaska_mc.name="alaska_duplicate"+alaska_inc;

                         alaska_inc++;

                         alaska_mc.startDrag(false);

                        alaska_mc.addEventListener(MouseEvent.MOUSE_UP, stdrag);

                         alaska_mc.addEventListener(MouseEvent.MOUSE_UP, stdrag);

                }

                if (event.currentTarget.name=="bg_load1") {

                         var lake_mc:lake_png=new lake_png();

                         addChild(lake_mc);

                         lake_mc.x=lake_mc.mouseX;

                         lake_mc.y=lake_mc.mouseY;

                         lake_mc.name="lake_duplicate"+lake_inc;

                         lake_inc++;

                         lake_mc.startDrag(false);

                        

                         lake_mc.addEventListener(MouseEvent.MOUSE_UP, stdrag);

                }

                if (event.currentTarget.name=="bg_load2") {

                         var mountn_mc:mountn_png=new mountn_png();

                         addChild(mountn_mc);

                         mountn_mc.x=mountn_mc.mouseX;

                         mountn_mc.y=mountn_mc.mouseY;

                         mountn_mc.name="mountn_duplicate"+mountn_inc;

                         mountn_inc++;

                         mountn_mc.startDrag(false);

                        

                         mountn_mc.addEventListener(MouseEvent.MOUSE_UP, stdrag);

                }

                if (event.currentTarget.name=="bg_load3") {

                         var town_mc:town_png=new town_png();

                         addChild(town_mc);

                         town_mc.x=town_mc.mouseX;

                         town_mc.y=town_mc.mouseY;

                         town_mc.name="town_duplicate"+town_inc;

                         town_inc++;

                         town_mc.startDrag(false);

                        

                         town_mc.addEventListener(MouseEvent.MOUSE_UP, stdrag);

                }

                if (event.currentTarget.name=="bg_load4") {

                         var water_mc:water_png=new water_png();

                         addChild(water_mc);

                         water_mc.x=water_mc.mouseX;

                         water_mc.y=water_mc.mouseY;

                         water_mc.name="water_duplicate"+water_inc;

                         water_inc++;

                         water_mc.startDrag(false);

                        water_mc.addEventListener(MouseEvent.MOUSE_UP, stdrag);

                }

                if (event.currentTarget.name=="bg_load5") {

                         var city_mc:city_png=new city_png();

                         addChild(city_mc);

                         city_mc.x=city_mc.mouseX;

                         city_mc.y=city_mc.mouseY;

                         city_mc.name="city_duplicate"+city_inc;

                         city_inc++;

                         city_mc.startDrag(false);

                        city_mc.addEventListener(MouseEvent.MOUSE_UP, stdrag);

                }

                if (event.currentTarget.name=="bg_load6") {

                          var citywide_mc:citywide_png=new citywide_png();

                         addChild(citywide_mc);

                         citywide_mc.x=citywide_mc.mouseX;

                         citywide_mc.y=citywide_mc.mouseY;

                         citywide_mc.name="citywide_duplicate"+citywide_inc;

                         citywide_inc++;

                         citywide_mc.startDrag(false);

                        citywide_mc.addEventListener(MouseEvent.MOUSE_UP, stdrag);

                }

                if (event.currentTarget.name=="bg_load7") {

                         var downtown_mc:downtown_png=new downtown_png();

                         addChild(downtown_mc);

                         downtown_mc.x=downtown_mc.mouseX;

                         downtown_mc.y=downtown_mc.mouseY;

                         downtown_mc.name="downtown_duplicate"+downtown_inc;

                         downtown_inc++;

                         downtown_mc.startDrag(false);

                        downtown_mc.addEventListener(MouseEvent.MOUSE_UP, stdrag);

                }

                if (event.currentTarget.name=="bg_load8") {

                         var powerlines_mc:powerlines_png=new powerlines_png();

                         addChild(powerlines_mc);

                         powerlines_mc.x=powerlines_mc.mouseX;

                         powerlines_mc.y=powerlines_mc.mouseY;

                         powerlines_mc.name="powerlines_duplicate"+powerlines_inc;

                         powerlines_inc++;

                         powerlines_mc.startDrag(false);

                        powerlines_mc.addEventListener(MouseEvent.MOUSE_UP, stdrag);

                }

                if (event.currentTarget.name=="bg_load9") {

                         var tropical_mc:tropical_png=new tropical_png();

                         addChild(tropical_mc);

                         tropical_mc.x=tropical_mc.mouseX;

                         tropical_mc.y=tropical_mc.mouseY;

                         tropical_mc.name="tropical_duplicate"+tropical_inc;

                         tropical_inc++;

                         tropical_mc.startDrag(false);

                        tropical_mc.addEventListener(MouseEvent.MOUSE_UP, stdrag);

                }

                if (event.currentTarget.name=="bg_load10") {

                         var waters_mc:waters_png=new waters_png();

                         addChild(waters_mc);

                         waters_mc.x=waters_mc.mouseX;

                         waters_mc.y=waters_mc.mouseY;

                         waters_mc.name="waters_duplicate"+waterthumb_inc;

                         waterthumb_inc++;

                         waters_mc.startDrag(false);

                        waters_mc.addEventListener(MouseEvent.MOUSE_UP, stdrag);

                }

               

      }

       

       

      Please suggest me if any  one.

       

       

       

       


      Regards,

       

      Viji. S