ForumsProgramming ForumGrid System & Placing objects into the grid (as2)

12 3098
mightybob
offline
mightybob
363 posts
1,060

I found several ways to do this but no one was very detailed, basically they all just said "copy and paste my awesome code and watch the magic happen".

Was wondering how I could do this⦠Any ideas?

  • 12 Replies
weirdlike
offline
weirdlike
1,309 posts
28,720

well with as3 this can be achieved by using the for loop 2 times, once for the rows and again for the columns

if you wanted a chessboard make 1 black square with the instance name of tileSquare
example:

//how many tiles in row and column
private var rows:Number = 8;
private var columns:Number = 8;

//empty movieclip with tile
private var board:MovieClip = new MovieClip();
private var tile:tileSquare = new tileSquare();

function main():void
{
//add the empty movieclip
board = new MovieClip();
addChild(board);

//for loop rows
for(var rows:int=0;rows

weirdlike
offline
weirdlike
1,309 posts
28,720

html conflicts with programming 2nd try

well with as3 this can be achieved by using the for loop 2 times, once for the rows and again for the columns

if you wanted a chessboard make 1 black square with the instance name of tileSquare
example:

//how many tiles in row and column
private var rows:Number = 8;
private var columns:Number = 8;

//empty movieclip with tile
private var board:MovieClip = new MovieClip();
private var tile:tileSquare = new tileSquare();

function main():void
{
//add the empty movieclip
board = new MovieClip();
addChild(board);

//for loop rows
for(var rows:int=0;rows&lt8;rows++)
{
//for loop columns
for(var columns:int=0;columns&lt8;columns++)
{
//check if odd then even
if(numParity(rows) == "odd" && numParity(columns) == "even&quot
{
//add tile
tile = new tileSquare();
tile.x = rows*tile.width+tile.width*.5;
tile.y = columns*tile.height+tile.height*.5;
board.addChild(tile);
}
//check again but if even then odd
else if(numParity(rows) == "even" && numParity(columns) == "odd&quot
{
//add tile
tile = new tileSquare();
tile.x = rows*tile.width+tile.width*.5;
tile.y = columns*tile.height+tile.height*.5;
board.addChild(tile);
}
}
}
}
//check if even or odd function
function numParity(num:Number):String
{
var parity:String = new String();
num % 2 ? parity = "odd" : parity = "even";
return parity;
}


at that point you can reference any tile by checking the row and column values

hope this helps

SOURCE

mightybob
offline
mightybob
363 posts
1,060

This looks like exactly what I'd need! Thanks so much!

I'll tell you how it goes, also, is the even & odd code required or is that just extra(ish)?

Still, it all looks pretty simple to turn into as2, thanks a lot!

weirdlike
offline
weirdlike
1,309 posts
28,720

The even/odd function returns weather the row and column is even or odd which I used simply to check for alternating tiles. So yes it is extra just to determine the placement of the black tiles

mightybob
offline
mightybob
363 posts
1,060

I got it working, but what if I wanted all the tiles to be filled? like no empty white spaces.

weirdlike
offline
weirdlike
1,309 posts
28,720

this is not the best look at it but it gives you an idea of what is going on

http://2.bp.blogspot.com/-RLoClqj99KE/Uh-gD4W0UFI/AAAAAAAACEs/bHW-Xdc_Iyg/s1600/Grid.jpg

the first box is 1, 1 making it odd, odd. the one next to it on the right is 1, 2 making it odd, even

if you used my system you can simply just create a new MoveClip then check for the desired odd or even combination and add the MovieClip similar toadding the black tile

mightybob
offline
mightybob
363 posts
1,060

I'm not getting it, I mean, I understand even odd stuff, just not how I should go about doing this with your code.

I've tried copy and pasting the for loop that has "even" and "odd" inside them and messing with the combinations where even and odd are, but nothing is working. Also tried changing the placement of them where it multiplies by .5 to 1, 2, .25, and also tried messing with when it +'s the height and width by its own height and width.

Sorry for wasting your time, but I've gone thru all my ideas and need some more help.

weirdlike
offline
weirdlike
1,309 posts
28,720

no problem. Ill slim down the code

for(var rows:int=0;rows &lt 8;rows++)
{
trace(rows)
}

output = numbers 1 through 8 representing left to right

for(var columns:int=0;columns &lt 8;columns++)
{
trace(columns)
}

output = numbers 1 through 8 representing top to bottom


Combined this is just one black square with white border MovieClip with the AS linkage name of "tileSquare"

for(var rows:int=0;rows &lt 8;rows++)
{
for(var columns:int=0;columns &lt 8;columns++)
{
var tile:tileSquare = new tileSquare();
tile.x = rows*tile.width+tile.width*.5;
tile.y = columns*tile.height+tile.height*.5;
addChild(tile);

trace(rows)
trace(columns)
}
}

output = numbers 1 through 8 representing left to right
output = numbers 1 through 8 representing top to bottom


NOTE*if you copy paste my code here it wont work due to the &lt symbol, you need to type it in manually

weirdlike
offline
weirdlike
1,309 posts
28,720

for(rows=0;rows &lt 8;rows++)
{
for(columns=0;columns &lt 8;columns++)
{
var tileSquare:MovieClip = this.attachMovie("tileSquare", "tileSquare"+rows, this.getNextHighestDepth(), {_x:+tileSquare._width*rows, _y:+tileSquare._height*columns});
}
}

tested and works with as2

mightybob
offline
mightybob
363 posts
1,060

Thanks so much weirdlike! It's all working just as I needed it too now!

weirdlike
offline
weirdlike
1,309 posts
28,720

gridAS2.fla

if you want it

mightybob
offline
mightybob
363 posts
1,060

I actually decided to use as3 on this one, thanks though!

Showing 1-12 of 12