/ Published in: ActionScript 3
<p>This is a short way of positioning display objects in a grid layout. It makes use of the modulo operator (%) to position each display object along the x axis and the floor method of the Math class for the y position.</p>
<p>This example creates 20 instances of a custom display object called MyDisplayObject positioning each instance in a grid 5 columns wide. As you may have guessed, the two 5's in the snippet represent the number of columns in the grid.</p>
<p>This example creates 20 instances of a custom display object called MyDisplayObject positioning each instance in a grid 5 columns wide. As you may have guessed, the two 5's in the snippet represent the number of columns in the grid.</p>
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
for (var i:uint = 0; i < 20; i++) { var displayObject:MyDisplayObject = new MyDisplayObject(); displayObject.x = displayObject.width * ( i % 5 ); displayObject.y = displayObject.height * Math.floor( i / 5 ); addChild(displayObject); }