AS3: Create a grid of bottons


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. private function generateBoard(startX:Number,startY:Number,totalRows:Number,totalCols:Number,buttonSize:Number):void {
  2. buttons = new Array();
  3. var colCounter:uint;
  4. var rowCounter:uint;
  5. for(rowCounter = 0; rowCounter < totalRows; rowCounter++) {
  6. for(colCounter = 0; colCounter < totalCols; colCounter++) {
  7. var b:Button = new Button();
  8. b.x = startX + (colCounter*buttonSize);
  9. b.y = startY + (rowCounter*buttonSize);
  10. b.addEventListener(MouseEvent.CLICK, letterClicked);
  11. b.label = getRandomLetter().toUpperCase();
  12. b.setSize(buttonSize,buttonSize);
  13. b.name = "buttonRow"+rowCounter+"Col"+colCounter;
  14. addChild(b);
  15.  
  16. buttons.push(b);
  17. }
  18. }
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.