Skip navigation
Currently Being Moderated

multiple buttons one rollover function

Aug 31, 2012 11:04 AM

i have a buttons being created and I am trying to create a rollover state for them.  It would be the same for all there.  It only seems to work on the last button created and i am not sure why.  Anyone see a problem with this code?

 

CODE

function drawButtons():void

{

 

 

          for (var j:Number = 1; j<4; j++)

          {

 

 

                    var textLabel:TextField = new TextField();

                    button = new Sprite();

                    button.graphics.beginFill(0xCCCCCC);

                    // purple color;

                    button.graphics.drawRoundRect(730, 0, 80, 25, 10, 10);

                    // x, y, width, height, ellipseW, ellipseH;

                    button.graphics.endFill();

                    //button[j].y = 275;

                    this.addChild(button);

                    button.buttonMode = true;

                    button.useHandCursor = true;

                    button.mouseChildren = false;

                    textLabel.textColor = 0x000000;

                    textLabel.x = 720;

                    textLabel.selectable = false;

                    textLabel.defaultTextFormat = textFormat;

 

 

                    if (j == 1)

                    {

                              trace(j);

                              button.y = 275;

 

 

                              textLabel.text = "A";

                              button.addChild(textLabel);

                              //Add Answer Button Listner

                              button.addEventListener(MouseEvent.MOUSE_DOWN, answerA_MouseDown);

                              button.addEventListener(MouseEvent.ROLL_OVER, manageMouseOver);

                              //button.addEventListener(MouseEvent.ROLL_OVER, buttonRollOverHandler);

                              //button.addEventListener(MouseEvent.ROLL_OUT, buttonRollOutHandler);

                    }

                    if (j == 2)

                    {

                              trace(j);

                              button.y = 310;

                              textLabel.text = "B";

                              button.addChild(textLabel);

                              //Add Answer Button Listner

                              button.addEventListener(MouseEvent.MOUSE_DOWN, answerB_MouseDown);

                              button.addEventListener(MouseEvent.ROLL_OVER, manageMouseOver);

                    }

                    if (j == 3)

                    {

                              trace(j);

                              button.y = 345;

                              textLabel.text = "C";

                              button.addChild(textLabel);

                              //Add Answer Button Listner

                              button.addEventListener(MouseEvent.MOUSE_DOWN, answerC_MouseDown);

                              button.addEventListener(MouseEvent.ROLL_OVER, manageMouseOver);

                    }

          }

}

function manageMouseOver(event:MouseEvent):void

{

          //button.removeEventListener(MouseEvent.ROLL_OVER, manageMouseOver);

          button.addEventListener(MouseEvent.ROLL_OUT, manageMouseOut);

          trace("Button roll over!");

          this.button.graphics.beginFill(0xFF0000);

          this.button.graphics.drawRoundRect(730, 0, 80, 25, 10, 10);

          this.button.graphics.endFill();

 

 

}

function manageMouseOut(event:MouseEvent):void

{

          //button.removeEventListener(MouseEvent.ROLL_OUT, manageMouseOver);

          //button.addEventListener(MouseEvent.ROLL_OVER, manageMouseOut);

          trace("Button roll out!");

          this.button.graphics.beginFill(0xCCCCCC);

          this.button.graphics.drawRoundRect(730, 0, 80, 25, 10, 10);

          this.button.graphics.endFill();

 

 

}

 
Replies
  • Currently Being Moderated
    Aug 31, 2012 12:04 PM   in reply to TheScarecrow

    I don't se it in your code, but if you are declaring the "button" object outside of the function ( var button:Sprite; ), there will only be one by that name, the last one you create.

     

    You don't need to try to use the name to target the buttons in the event handlers.  The button that was clicked can be targeted using: event.currentTarget

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points