AS3 Fill Available Screen Area with Squares (Centred)


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

Taken from Christian Cantrell's Adobe article called 'Authoring mobile Flash content for multiple screen sizes'.


Copy this code and paste it in your HTML
  1. // Display as many blocks on the screen as will fit
  2. var BLOCK_SIZE:Number = 1;
  3. var BLOCK_BUFFER:uint = 3;
  4. var blockSize:uint = this.inchesToPixels(BLOCK_SIZE);
  5. var blockTotal:uint = blockSize + BLOCK_BUFFER;
  6. var cols:uint = Math.floor(this.stage.stageWidth / blockTotal);
  7. var rows:uint = Math.floor((this.stage.stageHeight) / blockTotal);
  8. var blockXStart:uint = (this.stage.stageWidth - ((cols * blockSize) + ((cols - 1) * BLOCK_BUFFER))) / 2;
  9. var blockX:uint = blockXStart;
  10. var blockY:uint = ((this.stage.stageHeight) - ((rows * blockSize) + ((rows - 1) * BLOCK_BUFFER))) / 2;
  11. for (var colIndex:uint = 0; colIndex < rows; ++colIndex) {
  12. for (var rowIndex:uint = 0; rowIndex < cols; ++rowIndex) {
  13. var block:Sprite = this.getBlock(blockSize);
  14. block.x = blockX;
  15. block.y = blockY;
  16. this.addChild(block);
  17. blockX += blockTotal;
  18. }
  19. blockY += blockTotal;
  20. blockX = blockXStart;
  21. }
  22.  
  23. function getBlock(blockSize:uint):Sprite
  24. {
  25. var block:Sprite = new Sprite();
  26. block.graphics.beginFill(0xAAC228);
  27. block.graphics.drawRect(0, 0, blockSize, blockSize);
  28. block.graphics.endFill();
  29. block.cacheAsBitmap = true;
  30. return block;
  31. }
  32.  
  33. function inchesToPixels(inches:Number):uint
  34. {
  35. return Math.round(Capabilities.screenDPI * inches);
  36. }

URL: http://www.adobe.com/devnet/flash/articles/authoring_for_multiple_screen_sizes.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.