3 Replies Latest reply on Nov 6, 2007 3:42 PM by estacado1

    How to set a Text component's click event in actionscript

    estacado1 Level 1
      How do I set my text component to be able to handle the click event? I tried:

      var webtext:Text = new Text;
      webtext.addEventListener(MouseEvent.CLICK,
      function(event:MouseEvent):void{openBrowserLink("www.mysite.com");});

      private function openBrowserLink(url:String):void {
      var urlRequest:URLRequest = new URLRequest(url);
      navigateToURL(urlRequest, "_blank");
      }

      But this doesn't work. Then I tried it like this:

      var webtext:Text = new Text;
      webtext.addEventListener(MouseEvent.CLICK, openBrowserLink("www.mysite.com"););

      still didn't work. Any solutions?
        • 1. Re: How to set a Text component's click event in actionscript
          atta707 Level 2
          try this:

          var webtext:Text = new Text();
          webtext.addEventListener(MouseEvent.CLICK, openBrowserLink);

          addEventListener takes a reference of a function and the first parameter of that function must be a subclass of Event.

          private function openBrowserLink(event:MouseEvent):void {
          var urlRequest:URLRequest = new URLRequest("www.yahoo.com");
          navigateToURL(urlRequest, "_blank");
          }

          should work.
          • 2. Re: How to set a Text component's click event in actionscript
            Gregory Lafrance Level 6
            This code works:

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">
            <mx:Script><![CDATA[
            import mx.controls.Text;
            private var webtext:Text = new Text;

            private function init():void {
            webtext.text = "Click for Google";
            webtext.addEventListener(MouseEvent.CLICK,
            function(event:MouseEvent):void{openBrowserLink(" http://www.google.com");});
            addChild(webtext);
            }

            private function openBrowserLink(url:String):void {
            var urlRequest:URLRequest = new URLRequest(url);
            navigateToURL(urlRequest, "_blank");
            }
            ]]></mx:Script>
            </mx:Application>
            • 3. How to set a Text component's click event in actionscript
              estacado1 Level 1
              Okay the solutions worked, but I'm getting the url from the Text component's id. This is because I have a lot of them. So, what I plan to use is:

              var webtext:Text = new Text;
              webtext.id = url;

              webText.addEventListener(MouseEvent.CLICK,
              function(event:MouseEvent):void{openBrowserLink(event.target.id);});

              The problem is the event.target.id statement. It doesn't work for a Text component. I tried it with a Button component, and it worked. Is there any equivalent of it for the Text component?


              ***Edit***
              I've found a solution. For Text component, use currentTarget instead if target.