13 Replies Latest reply on Jan 14, 2010 1:29 PM by kglad

    Dynamically display xml content based off addEventListener(MouseEvent.MOUSE_DOWN, displayContent);

    Chipleh Level 3

      Hi,

       

      I have been working with this script for a few days now and have gotten to a near completed point, but I can't seem to script the final step. I have an xml file that is being parsed into my .fla file. There are 3 buttons which are dynamically created based on a tag called <navItem>. The function 'displayContent' should be displaying the appropriate chunk of xml in various text boxes, but instead, it only displays the last chunk (the 3rd chunk) no matter which button I click on. I have pasted my code on pastebin here:

       

      http://pastebin.com/mb3e3cd9

       

      If anyone could take a look at my code, especially the last function, 'displayContent' and suggest what I could do to correct the code, or point out where I've gone wrong, I'd be quite thankful.

       

      ~Chipley

        • 1. Re: Dynamically display xml content based off addEventListener(MouseEvent.MOUSE_DOWN, displayContent);
          kglad Adobe Community Professional & MVP

          1.  your ith button doesn't know it's the ith button

           

          2.  in displayContent(), you're using the last value of i defined in your for-loop.  how do you expect that to work?

           

          to remedy, use movieclip buttons, assign a property to each button so it knows it's the ith button and retrieve that property in displayContenet().

          1 person found this helpful
          • 2. Re: Dynamically display xml content based off addEventListener(MouseEvent.MOUSE_DOWN, displayContent);
            Chipleh Level 3

            Thanks for the response kglad. I'm quite new to AS3, I've had some AS1 experience but years ago. I've been following lots of tutorials to get to this point over the past week and a half.

             

            1. How would I tell the ith button it is indeed the ith button?

             

            2. I was attempting to assign the for loop in displayContent to the appropriate button, but Chipley fail.

             

            I am using movie clip buttons but I guess my whole issue is retrieving the button identifier from displayContent and assign the proper xml chunk to be displayed.

             

            Thanks again, I have lots more studying to do in order to figure out your reply.

             

            Cheers to you,

            ~Chipley

            • 3. Re: Dynamically display xml content based off addEventListener(MouseEvent.MOUSE_DOWN, displayContent);
              kglad Adobe Community Professional & MVP

              IF you're using movieclip buttons:

               

              1.         myText.condenseWhite = true;
              2.                 var paragraphText = xml.mpu.bodyText.page.p;
              3.                 myText.htmlText = paragraphText;
              4.               
              5.                 trace(myText.text);                           
              6.                 for(var i=0;i<xml.mpu.bodyText.page.navItem.length();i++)
              7.                 {
              8.                         trace("xml.mpu.bodyText.page.navItem.length = " + i);                 
              9.                         var mcMenuItem:mcMenu=new mcMenu();
              10.                         mcMenuItem.x=button_mc.x;
              11.                         mcMenuItem.y= button_mc.y + i*50;
              12.                         addChild(mcMenuItem);
              13.                         mcMenuItem.btnTxt.htmlText = navItemText[i];
              14. mcMenuItem.ivar=i;
              15.                         mcMenuItem.buttonMode = true;                         
              16.                         mcMenuItem.addEventListener(MouseEvent.MOUSE_DOWN, displayContent);                   
              17.                 }                             
              18.         }
              19. }
              20. function displayContent(event:MouseEvent):void {
              21.         for(var i=0;i<xml.mpu.bodyText.page.navItem.length();i++)
              22.                 {                     
              23.                         var titleText = xml.mpu.bodyText.page.h1[event.currentTarget.ivar];
              24.                         myTitle.htmlText = titleText;
                                      var paragraphText = xml.mpu.bodyText.page.p[event.currentTarget.ivar];
              1.                         myText.htmlText = paragraphText;
              2.                 }
              3. }
              • 4. Re: Dynamically display xml content based off addEventListener(MouseEvent.MOUSE_DOWN, displayContent);
                Chipleh Level 3

                Ah, wow! Thank you very much kglad, that code certainly sorts it out.

                 

                I understand your use of ivar as the property, but I need to look up and study up on event.currentTarget (as I mentioned, AS3 syntax is still very new to me). I remember you helped out on various issues back in the 2003 - 2005 timeframe on Macromedia's forum.

                 

                Thanks again for your help, much appreciation.

                ~Chip

                • 5. Re: Dynamically display xml content based off addEventListener(MouseEvent.MOUSE_DOWN, displayContent);
                  kglad Adobe Community Professional & MVP

                  you're welcome.

                   

                  event.currentTarget is the object that dispatched the event.  ie, the clicked button.  each clicked button knows its the ith button because that value is store in the ivar property of each button.  that ivar property is retrieved in your listener function.

                  1 person found this helpful
                  • 6. Re: Dynamically display xml content based off addEventListener(MouseEvent.MOUSE_DOWN, displayContent);
                    Chipleh Level 3

                    Very useful. Just read up on currentTarget after reading your comment and applied to a few different functions I'm working on. That leads me to another question(for which i should probably start a new topic?). How do you reverse an event that is initiated with 'event.currentTarget'?

                     

                    i.e. - I have 3 buttons, and within the displayContent function, I am sending the currentTarget(one of the movie clip buttons) to frame 2. How do you send the currenttarget back to frame 1 with or without using event.currentTarget since currentTarget has already been initiated?

                    • 7. Re: Dynamically display xml content based off addEventListener(MouseEvent.MOUSE_DOWN, displayContent);
                      kglad Adobe Community Professional & MVP

                      you mean you want to direct the previous currentTarget back to its frame 1, correct?  if so, use a variable to store the value of the previous clicked button and use that variable:

                       

                      var previousClickedBtn:MovieClip;

                       

                      function displayContent(e:MouseEvent){

                      if(previousClickedBtn!=null){

                      previousClickedBtn.gotoAndStop(1);

                      }

                      previousClickedBtn=e.currentTarget;

                      e.currentTarget.gotoAndStop(2);

                      .

                      .

                      .

                      }

                      • 8. Re: Dynamically display xml content based off addEventListener(MouseEvent.MOUSE_DOWN, displayContent);
                        Chipleh Level 3

                        Thanks for that function kglad. I'm trying to implement it right now. I've made a modification and renamed the function to returnState, and am listening on the MOUSE_UP state, like so:

                         

                        mcMenuItem.addEventListener(MouseEvent.MOUSE_UP, returnState);

                         

                        (your function)

                         

                        function returnState(e:MouseEvent){
                        if(previousClickedBtn!=null){
                          previousClickedBtn.gotoAndStop(1);
                        }
                        previousClickedBtn=e.currentTarget;
                        e.currentTarget.gotoAndStop(2);
                        }

                         

                        I assume this function will work 'as is' once 'previousClickedBtn' is defined. I am having some confusion about where and how to define 'previousClickedBtn'. Based off you code in this thread that I marked as The Answer, where and how would you go about defining the 'previousClickedBtn' variable?:


                        var previousClickedBtn:MovieClip;

                        • 9. Re: Dynamically display xml content based off addEventListener(MouseEvent.MOUSE_DOWN, displayContent);
                          kglad Adobe Community Professional & MVP

                          that code should go in your mousedown listener function just like i showed it in displayContent:

                           

                           

                           

                          1.         myText.condenseWhite = true;
                          2.                 var paragraphText = xml.mpu.bodyText.page.p;
                          3.                 myText.htmlText = paragraphText;
                          4.              
                          5.                 trace(myText.text);                          
                          6.                 for(var i=0;i<xml.mpu.bodyText.page.navItem.length();i++)
                          7.                 {
                          8.                         trace("xml.mpu.bodyText.page.navItem.length = " + i);                
                          9.                         var mcMenuItem:mcMenu=new mcMenu();
                          10.                         mcMenuItem.x=button_mc.x;
                          11.                         mcMenuItem.y= button_mc.y + i*50;
                          12.                         addChild(mcMenuItem);
                          13.                         mcMenuItem.btnTxt.htmlText = navItemText[i];
                          14. mcMenuItem.ivar=i;

                          15.                         mcMenuItem.buttonMode = true;                        
                          16.                         mcMenuItem.addEventListener(MouseEvent.MOUSE_DOWN, displayContent);                  
                          17.                 }                            
                          18.         }
                          19. }
                          20. function displayContent(event:MouseEvent):void {
                          21. if(previousClickedBtn!=null){

                            previousClickedBtn.gotoAndStop(1);

                            }

                            previousClickedBtn=event.currentTarget;

                          22.         for(var i=0;i<xml.mpu.bodyText.page.navItem.length();i++)

                          23.                 {                    

                          24.                         var titleText = xml.mpu.bodyText.page.h1[event.currentTarget.ivar];

                          25.                         myTitle.htmlText = titleText;

                                                  var paragraphText = xml.mpu.bodyText.page.p[event.currentTarget.ivar];

                          1.                         myText.htmlText = paragraphText;
                          2.                 }
                          3. }
                          • 10. Re: Dynamically display xml content based off addEventListener(MouseEvent.MOUSE_DOWN, displayContent);
                            Chipleh Level 3

                            Niceness! I got that working, although it wasn't exactly what i was trying to do, my explanation was quite shotty.

                             

                            Imagine 3 buttons.

                            1. Click on button1; button1 goes to frame 2.

                            2. Click on button2; button2 goes to frame 2 & button1 goes back to frame1

                            3. Click on button3; button3 goes to frame 2 & button2 goes back to frame1

                            4. So on and so forth...

                             

                            I am capturing the name of the movie clip that i want to send back to frame 1 in a dynamic text box on the stage named 'returnBtn'. I capture this on the MOUSE_UP event. I can see this data and the names being captured are working properly. In the displayContent function, I can see this data being traced properly as well. My issue, I believe, is that I don't have the proper syntax to contruct the returnBtn into the targeted movie clip and send it back to frame 1. (hope that makes sense)

                             

                            //Here's my hacked code:

                             

                            mcMenuItem.addEventListener(MouseEvent.MOUSE_DOWN, displayContent); 
                            mcMenuItem.addEventListener(MouseEvent.MOUSE_UP, returnState);  

                             

                             

                             

                            function displayContent(event:MouseEvent):void {
                             

                            event.currentTarget.gotoAndStop(2);

                            var returnBtn = prevBtn.text.toString();

                            trace("returnBtn = " + returnBtn);
                            returnBtn.gotoAndStop(1);
                            }


                            function returnState(event:MouseEvent):void{

                            trace("returnState");
                            var previousClickedBtn=event.currentTarget;
                            prevBtn.text = previousClickedBtn.name;
                            trace("previousClickedBtn = " + previousClickedBtn.name);

                            }

                             

                             

                            *and just to reiterate, displayContent is sending the currentTarget that's clicked to frame2, which is working, no problem. What I'm trying to do is send the movie clip that is captured in var returnBtn back to frame 1.

                            • 11. Re: Dynamically display xml content based off addEventListener(MouseEvent.MOUSE_DOWN, displayContent);
                              kglad Adobe Community Professional & MVP
                              var returnBtn:MovieClip;

                               

                              mcMenuItem.addEventListener(MouseEvent.MOUSE_DOWN, displayContent); 
                              mcMenuItem.addEventListener(MouseEvent.MOUSE_UP, returnState);  

                               

                               

                               

                              function displayContent(event:MouseEvent):void {

                              event.currentTarget.gotoAndStop(2);

                              returnBtn.gotoAndStop(1);

                              returnBtn = event.currentTarget;

                               

                              var returnBtn = prevBtn.text.toString();

                              trace("returnBtn = " + returnBtn);
                              }

                               

                              • 12. Re: Dynamically display xml content based off addEventListener(MouseEvent.MOUSE_DOWN, displayContent);
                                Chipleh Level 3

                                Thank you again for all your help on this on kglad, much appreciation!

                                 

                                ~Chip