Skip navigation
Currently Being Moderated

Draw Multiple Buttons

Aug 31, 2012 7:40 AM

Is it possible to use a For loop to create 3 buttons on the stage and add text to them?  This is what I have been working with but it just creates one button and i am not sure why.  Can anyone help?  ( I have added button[j] to some of them and I get an error when I do that is ReferenceError: Error #1069: Property 1 not found on flash.display.Sprite and there is no default value.) thanks.

 

CODE EX.

function drawButtons():void

{

          var textLabel:TextField = new TextField();

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

          {

 

 

                    button.graphics.clear();

                    button = new Sprite();

                    button.graphics.beginFill(0x000000);

                    // purple color;

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

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

                    button.graphics.endFill();

                    button.x = 725;

                    //button[j].y = 275;

                    this.addChild(button);

                    button.buttonMode = true;

                    button.useHandCursor = true;

                    button.mouseChildren = false;

                    textLabel.textColor = 0xFFFFFF;

                    textLabel.x = 10;

                    textLabel.selectable = false;

                    textLabel.defaultTextFormat = textFormat;

                    this.button.addChild(textLabel);

                    if (j == 1)

                    {

                              trace(j);

                              button.y = 275;

                              textLabel.text = "A";

                              textLabel.y = 5;

                              //Add Answer Button Listner

                              button.addEventListener(MouseEvent.MOUSE_DOWN, answerA_MouseDown);

                    }

                    if (j == 2)

                    {

                              trace(j);

                              button.y = 300;

                              textLabel.text = "B";

                              textLabel.y = 5;

                              //Add Answer Button Listner

                              button.addEventListener(MouseEvent.MOUSE_DOWN, answerB_MouseDown);

                    }

                    if (j == 3)

                    {

                              trace(j);

                              button.y = 325;

                              textLabel.text = "C";

                              textLabel.y = 5;

                              //Add Answer Button Listner

                              button.addEventListener(MouseEvent.MOUSE_DOWN, answerC_MouseDown);

                    }

          }

}

 
Replies
  • kglad
    72,235 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 31, 2012 7:52 AM   in reply to TheScarecrow

    remove that clear(), make sure you're calling drawButtons and make sure your stage is large enough (esp width) to see those buttons.

     
    |
    Mark as:
  • kglad
    72,235 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 31, 2012 7:53 AM   in reply to TheScarecrow

    remove:

     

       button.graphics.clear();

     

    and make sure your stage is large enough (~760x350) to see your buttons.

     
    |
    Mark as:
  • kglad
    72,235 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 31, 2012 7:58 AM   in reply to TheScarecrow

    use:

     

    CODE EX.

    function drawButtons():void

    {

             // var textLabel:TextField = new TextField();

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

              {

     

    var textLabel:TextField = new TextField();

     

     

    p.s.  please mark helpful/correct responses.

     
    |
    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