Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Simple hit testing would be an easy solution, but could run you into problems if there are many buttons and only limited space for these. Depending on the situation, a better (and more complicated) solution might be to rasterize the space and place a button into (nearly) every cell with a random shift:</p> <pre><code>// environment variables const numButtons:int = 16; const maxButtonWidth:int = DummyButton.MAX_WIDTH; const maxButtonHeight:int = DummyButton.MAX_HEIGHT; const width:Number = stage.stageWidth; const height:Number = stage.stageHeight; // pseudo-random positioning // ratio is needed for a proportional grid var areaRatio:Number = width / height; var maxButtonRatio:Number = maxButtonWidth / maxButtonHeight; // compute buttons per column and row (take aspect ratios into account) var cols:int = Math.floor(Math.sqrt(numButtons) * (areaRatio / maxButtonRatio)); var rows:int = Math.ceil(numButtons / cols); // available space for each button within a grid segment var spaceX:Number = (width - maxButtonWidth * cols) / cols; var spaceY:Number = (height - maxButtonHeight * rows) / rows; // throw an error if buttons won't fit (based on maximum dimensions) if (cols &lt; 1 || rows &lt; 1 || spaceX &lt; 0 || spaceY &lt; 0) { throw new Error("buttons do not fit into area"); } // for every button for (var i:int = 0; i &lt; numButtons; i++) { var row:int = Math.floor(i / cols); var col:int = i % cols; var button:Sprite = new DummyButton(); // coordinate = grid segment + random shift button.x = col * (maxButtonWidth + spaceX) + Math.random() * spaceX; button.y = row * (maxButtonHeight + spaceY) + Math.random() * spaceY; stage.addChild(button); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload