Forums → Programming Forum → Grid System & Placing objects into the grid (as2)
12 | 7889 |
- 12 Replies
Showing 1-12 of 12
12 | 7889 |
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?
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
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<8;rows++)
{
//for loop columns
for(var columns:int=0;columns<8;columns++)
{
//check if odd then even
if(numParity(rows) == "odd" && numParity(columns) == "even"
{
//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"
{
//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
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!
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
I got it working, but what if I wanted all the tiles to be filled? like no empty white spaces.
this is not the best look at it but it gives you an idea of what is going on
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
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.
no problem. Ill slim down the code
for(var rows:int=0;rows < 8;rows++)
{
trace(rows)
}
output = numbers 1 through 8 representing left to right
for(var columns:int=0;columns < 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 < 8;rows++)
{
for(var columns:int=0;columns < 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 < symbol, you need to type it in manually
for(rows=0;rows < 8;rows++)
{
for(columns=0;columns < 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
Thanks so much weirdlike! It's all working just as I needed it too now!
You must be logged in to post a reply!