7 Replies Latest reply: Jan 3, 2010 2:56 PM by kglad RSS

    Convert Loader() to SimpleButton()

    EntWebDS Community Member

      I am loading a series of images from an XML file into Loader() objects, but I am having some issues with what I want to do with those images afterwards.

       

      I need to set the size of the images to thumbnails for a sliding chooser and since the images are various widths, I cannot use a constant.  I have to get the Bitmap from the contentLoaderInfo:

       

      //This is found within an array loop and loads for each picture in the XML file (actually, every other pic)

              picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadChooserImages);

       

      function loadChooserImages(evt:Event):void
      {

          // All thumbmails will have a height of 50.

           // The height will be proportionally set.

           var bm:Bitmap = Bitmap(evt.target.content);

           var reductionVal = 50 / bm.height;

           bm.width = bm.width * reductionVal;       
           bm.height = 50; 

           leftX = bm.width; // leftX is the x value that will be used to place the next image

           // intChooserWidth is the width of all the images plus the outer margins

           // intSpace is the space between images images

           intChooserWidth = leftX + intSpace;
      }

       

      The problems are

      1)

      a) the objects do not trigger the COMPLETE event in the same order as the array because some images are larger than others, so my row of images is not in the same order as the array.  How do I get them in the correct order?

      b) intChooserWidth, though declared at the module level of frame 1 of the .fla is not seen by the child object code that runs the slider.  How does one access a variable that was declared and set in a parent object?

       

      2) I want to place the children  as SimpleButtons and assign Event Listeners. I do not know how to recast the Loader() object as a SimpleButton.

       

      I have attached the .fla and xml files but not the graphic files.  Any files will do as long as the XML file matches the image names.

       

      Any help would be appreciated.

        • 1. Re: Convert Loader() to SimpleButton()
          kglad CommunityMVP

          1a.  load sequentially by starting to load your first and using the complete event to trigger loading the next, if you haven't loaded them all.

           

          1b.  use parent.intChooserWidth

           

          2.  add each loader to a movieclip or sprite and assign listeners to the parent movieclip or sprite.

          • 2. Re: Convert Loader() to SimpleButton()
            EntWebDS Community Member

            1a I will try this solution.

             

            1b I tried parent.intChooserWidth but I get a runtime compile error:

            1119: Access of possibly undefined property intChooserWidth through a reference with static type flash.display:DisplayObjectContainer.

             

            Perhaps the variable isn't instantiated yet?

             

            2. Do I create a SimpleButton object and addChild() to the SimpleButton?

                    var new_btn:SimpleButton = new SimpleButton();
                    this.strip_mc.buttonsMenu.addChild(new_btn);
                    this.strip_mc.buttonMenu.new_btn.addChild(picLoader);
            This code causes an error:


            Error #1010: A term is undefined and has no properties.

             

            Thanks for your help!

            • 3. Re: Convert Loader() to SimpleButton()
              kglad CommunityMVP

              1b.  cast the parent as a movieclip:

               

              MovieClip(parent).intChooserWidth

               

              2.  use a movieclip.  there's no perceptible downside and there are substantial benefits.  and, use:

               

              var new_btn:MovieClip= new MovieClip();
                      this.strip_mc.buttonsMenu.addChild(new_btn);
                      new_btn.addChild(picLoader);

              • 4. Re: Convert Loader() to SimpleButton()
                EntWebDS Community Member

                Everything worked as advertised.  Thank you.  However, I still have an issue.

                 

                I have the following code:

                 

                            var new_btn:MovieClip = new MovieClip();
                            this.strip_mc.buttonMenu.addChild(new_btn);
                            new_btn.name = "pic" + intLoop;
                            new_btn.addEventListener(MouseEvent.CLICK, onSelectPhoto);
                            // Add the pic to the button (movie clip, actually)
                            new_btn.addChild(picLoader);

                 

                The highlighted line names the button so that I can identify it in the onSelectPhoto event, but evt.target.name in the handler gives me a name such as "instance34" instead of the name I gave it. What can be done?

                 

                Message was edited by: Lonesome61 trace(evt.target) returns [object Loader]  I thought it should have returned a MovieClip object since new_btn is a MovieClip.   ???

                • 5. Re: Convert Loader() to SimpleButton()
                  kglad CommunityMVP

                  use currentTarget instead of target.

                  • 6. Re: Convert Loader() to SimpleButton()
                    EntWebDS Community Member

                    That worked.  I knew nothing of the currentTarget object.  Thanks.

                     

                    evt.target.parent.name worked as well.

                    • 7. Re: Convert Loader() to SimpleButton()
                      kglad CommunityMVP

                      you're welcome.