11 Replies Latest reply on May 21, 2010 11:36 AM by kglad

    the new getURL method for AS3...

    r_tist Level 1

      Hi,

       

      Using AS3, how can I attach links to outbound websites? I have a button graphic dragged onto the timeline. It is meant to function much like a "hot spot". So, I would like to attach the script to the button so that it will link to another website when clicked upon. But where do I add the script in AS3, and what is that script to be? ~ For example, do I need to go into edit mode where I see the 4 states, or can I simply attach it to the main timeline? - I'm looking to do whatever's the simplest! From what I have read, it goes onto the main timeline.

       

      Also, the button is called myButton in my library...can I use that same button for everywhere I want to add an outbound link to different websites, or am I going to have to change the name for every different link? For example, myButton02, myButton03? -- I'm hoping to use the same graphic button, and hoping the timeline will understand and follow the script instead of being confused and all links wind up going to the same site.

       

      Here is the script I found online that might work?

       

      myButton.addEventListener(MouseEvent.CLICK, onMouseClick);

      function onMouseClick(e:MouseEvent):void
      {
      var request:URLRequest = new URLRequest("http://www.google.com");
      navigateToURL(request);
      }

       

      Please advise! Thank you! I cannot get to work and have found other variations of this code online.

        • 1. Re: the new getURL method for AS3...
          kglad Adobe Community Professional & MVP

          your code looks good.  any problem?

          • 2. Re: the new getURL method for AS3...
            r_tist Level 1

            Hi,

             

            OK, I am half way there! I got the first button hotspot to link to an outbound site OK (hooray!). Now, I tried two things for the next button hotspot - 1) Tried using the same button as the first one, but giving it a different instance name only (in properties panel) and then calling upon that different instance name.

            - That didn't work; it detected a duplicate. 2) tried duplicating the button in the library and re-naming it. Then dragging the new "button" hotspot to the stage, giving it yet another different instance name (in properties inspector) and calling upon the new name from within the script. - Still detecting a duplicate when I go to publish.

             

            I'm getting: Scene 1, Layer 'actions', Frame 500, Line 3    1021: Duplicate function definition     function onMouseClick(e:MouseEvent):void

             

            ~ Can you please point me in the right direction? Almost got it figured.

             

            Thank you!

            • 3. Re: the new getURL method for AS3...
              kglad Adobe Community Professional & MVP

              onMouseClick appears more than once in your code.  use movie explorer to find the duplicate and rename or remove the duplicate.

              • 4. Re: the new getURL method for AS3...
                r_tist Level 1

                Hi,

                 

                I need it to appear twice, in fact, I will need it to appear multiple times because there will be multiple buttons which use the onMouseClick command.

                What should I do in this case? Should I be citing the script differently?

                 

                Thanks!

                • 5. Re: the new getURL method for AS3...
                  kglad Adobe Community Professional & MVP

                  no you don't.   you only need to define a function once.  it can be called many times using many different listeners applied to many different objects.

                  • 6. Re: the new getURL method for AS3...
                    r_tist Level 1

                    So, for example, what would the script be for the second button in order to link to an outbound site? What would the script be for the 3rd button.

                    OnMouseClick, I need each different link to link to each different website, follow me?

                     

                    What should it look like if this is the case??

                     

                    Thanks!

                    • 7. Re: the new getURL method for AS3...
                      kglad Adobe Community Professional & MVP

                      if you're using movieclip buttons, apply a property to each button to indicate the url and use that in the listener function.

                       

                      if you're using simple buttons, you'll have to call a different listener function for each button or create an associative array that associates each button's name property with the corresponding url.

                      • 8. Re: the new getURL method for AS3...
                        r_tist Level 1

                        if you're using movieclip buttons, apply a property to each button to indicate the url and use that in the listener function

                         

                        - Can you please give me an example for both: Can you show me the script based upon what I had quoted above so

                        I can have an "aha" moment? - So I can view how I need to alter it code-wise? I need you to use my script and show me vs. explaining

                        to me in this manner.

                         

                        Thank you. And I tried re-naming the property and also duplicating the button in the library with a different name + a different

                        property name, and I still cannot get it to work properly. - Also, am I making a button mc for every button, or am I recycling

                        the button mc but changing the script + property name only?

                        • 9. Re: the new getURL method for AS3...
                          kglad Adobe Community Professional & MVP

                          for movieclip buttons:

                           

                          mc1.urlS = "http://www.adobe.com";

                          mc2.urlS = "http://www.kglad.com";

                          etc.


                          mc1.addEventListener(MouseEvent.CLICK,f);

                          mc2.addEventListener(MouseEvent.CLICK,f);


                          function f(e:MouseEvent){

                          navigateToURL(new URLRequest(MovieClip(e.currentTarget).urlS));

                          }

                           

                          for simple buttons (not in the same fla as above code):

                           

                          var urlObj:Object={};

                          urlObj[btn1.name]="http://www.adobe.com";

                          urlObj[btn2.name]="http://www.kglad.com";

                          etc


                          btn1.addEventListener(MouseEvent.CLICK,f);

                          btn2.addEventListener(MouseEvent.CLICK,f);


                          function f(e:MouseEvent){

                          navigateToURL(new URLRequest(urlObj[e.currentTarget.name]));

                          }

                          • 10. Re: the new getURL method for AS3...
                            r_tist Level 1

                            Aha, this makes sense to me!

                             

                            I will work on!

                             

                            Thank you!!

                            • 11. Re: the new getURL method for AS3...
                              kglad Adobe Community Professional & MVP

                              you're welcome.

                               

                              p.s.  please mark this thread as answered.