5 Replies Latest reply: Aug 31, 2012 7:58 AM by kglad RSS

    Draw Multiple Buttons

    TheScarecrow Community Member

      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);

                          }

                }

      }