/ Published in: ActionScript 3
Expand |
Embed | Plain Text
private function generateBoard(startX:Number,startY:Number,totalRows:Number,totalCols:Number,buttonSize:Number):void { buttons = new Array(); var colCounter:uint; var rowCounter:uint; for(rowCounter = 0; rowCounter < totalRows; rowCounter++) { for(colCounter = 0; colCounter < totalCols; colCounter++) { var b:Button = new Button(); b.x = startX + (colCounter*buttonSize); b.y = startY + (rowCounter*buttonSize); b.addEventListener(MouseEvent.CLICK, letterClicked); b.label = getRandomLetter().toUpperCase(); b.setSize(buttonSize,buttonSize); b.name = "buttonRow"+rowCounter+"Col"+colCounter; addChild(b); buttons.push(b); } } }
Comments
Subscribe to comments
You need to login to post a comment.

Nice! I have written a nice little class that builds a grid out of supplied display objects or class of your choice.. I have also explained the code so that you can get a foundation of how to start creating grids for building puzzle type games. Check it out: http://www.as3blog.org/?p=50
Code snippet - AS3: Create a grid of bottons on Snipplr
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 + (colCounterbuttonSize); 9. b.y = startY + (rowCounterbuttonSize); 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. }