6 Replies Latest reply on Sep 23, 2010 6:45 AM by Linda Valentin

    Textfield Loop - Rollover and Clickevents

    Jonas Philippon

      Hi.

       

      I have created a loop with 5 textfields (with 5 different names). The textfields autosizes and are in an array, so i can place every textfield as i want.

       

      - But how do i create rollover and click events to these textfields?

       

      - Is it possible to place this loop in a movieclip, where the movieclip autosizes according to the text in my text-array?

       

      This is for a menu only with text - no graphic.

       

      My Code:

       

      // INITIAL VARIABLES

      var buttonArray:Array = new Array( );

      var currentButton:Object=new Object;

       

      // NAVIGATION CONTAINER

      var navContainer:Sprite=new Sprite  ;

      addChild(navContainer);

       

      // NAMES OF BUTTONS

      var navArray:Array=["BUTTON 1","BUTTON 2","BUTTON 3","BUTTON 4","BUTTON 5"];

       

      // NAVIGATION BUTTONS

      var myFont:Font = new GillSansLight();

       

      var NavItemFormat:TextFormat = new TextFormat();

      NavItemFormat.font = myFont.fontName;

      NavItemFormat.size = 13;

      NavItemFormat.color = 0xCCCCCC;

      NavItemFormat.letterSpacing = 2;

       

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

       

      var NavItem:TextField = new TextField()

      NavItem.autoSize = TextFieldAutoSize.LEFT;

      NavItem.defaultTextFormat = NavItemFormat;

      NavItem.embedFonts = true;

      NavItem.text=navArray[i];

      buttonArray.push(NavItem);

      //NavItem.addListeners();

      navContainer.addChild(NavItem);

       

      NavItem.name=String(i);

      }

       

      navContainer.buttonMode = true;

       

      navContainer.mouseChildren = false;

       

       

       

      Philippon

        • 2. Re: Textfield Loop - Rollover and Clickevents
          rHemanth Level 2

          import flash.events.MouseEvent;

           

          var buttonArray:Array = new Array( );

          var currentButton:Object=new Object;

          var yPos:Number=10;

          // NAVIGATION CONTAINER

          var navContainer:Sprite=new Sprite  ;

          navContainer.x=100;

          navContainer.y=100;

          addChild(navContainer);

           

          // NAMES OF BUTTONS

          var navArray:Array=["BUTTON 1","BUTTON 2","BUTTON 3","BUTTON 4","BUTTON 5"];

           

          // NAVIGATION BUTTONS

          //var myFont:Font = new GillSansLight();

           

          var NavItemFormat:TextFormat = new TextFormat();

          NavItemFormat.font = 'Sans';

          NavItemFormat.size = 30;

          NavItemFormat.color = 0x000000;

          NavItemFormat.letterSpacing = 2;

           

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

          trace("in");

          var NavItem:TextField = new TextField();

          NavItem.autoSize = TextFieldAutoSize.LEFT;

          NavItem.defaultTextFormat = NavItemFormat;

          NavItem.selectable = false;

          NavItem.text=navArray[i];

          buttonArray.push(NavItem);

          //NavItem.addListeners();

          NavItem.y=yPos;

          yPos+=35;

          navContainer.addChild(NavItem);

           

          NavItem.name=String(i);

          NavItem.addEventListener(MouseEvent.CLICK,onClick);

          NavItem.addEventListener(MouseEvent.ROLL_OVER,onRoll);

          }

           

          navContainer.buttonMode = true;

           

          //navContainer.mouseChildren = false;

           

          function onClick(e:MouseEvent):void{

          trace("CLICK : "+e.target.name);

          }

           

          function onRoll(e:MouseEvent):void{

          trace("ROLL :"+e.target.name);

          }

           

          Based on the name of the text field u can have  various operations no need to add  the text field to separate movie clip and resize,u can add mouse events to text field too

          1 person found this helpful
          • 3. Re: Textfield Loop - Rollover and Clickevents
            Jonas Philippon Level 1

            Hi.

             

            Thank you for your answer (:

             

            I worked a little and made it work (: Thank you very much.

             

            - Can you maybe tell me how to put all the textfields in separate MovieClips?

             

            - And how do I make a function removeListeners():void {  } I need the textfields to remove listeners on click. But when i add the listeners, i do it in the NavItem loop. I cant do that with remove listeners?

             

            Thank you again (:

            • 4. Re: Textfield Loop - Rollover and Clickevents
              riquigley

              There are several ways you could do it.  I would try adding in the Remove Listener function in the handler for the clicks.  So, for instance:

               

              function onClick(e:MouseEvent):void{

              trace("CLICK : "+e.target.name);

               

              var tempTextField:TextField = e.target;  // this makes a reference to the TextField that was clicked

                    tempTextField.removeEventListener(MouseEvent.CLICK,onClick);  // this removes the listener

              }

               

              Unfortunately, I don't have spare time to test it and see if assigning a TextField to another text field creates a reference to the original textfield (like it does when you create a reference to a MovieClip), or if it makes a clone of the original (new textfield, just with all the same properties of the old one).

               

              If it clones it, then the above won't work.  In which case, try this:

               

              function onClick(e:MouseEvent):void{

              trace("CLICK : "+e.target.name);

                    e.target.removeEventListener(MouseEvent.CLICK,onClick);  // this removes the listener

              }

              • 5. Re: Textfield Loop - Rollover and Clickevents
                Jonas Philippon Level 1

                Hi.

                 

                I actually got it to work. I used your last example - i havent tried the first (:

                 

                Thank you.

                • 6. Re: Textfield Loop - Rollover and Clickevents
                  Linda Valentin

                  Thanks a ton for your response. I'll write back and let you know if it works.