Forums

ForumsProgramming Forum

Grid System & Placing objects into the grid (as2)

Posted Feb 17, '14 at 7:05pm

mightybob

mightybob

353 posts

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?

 

Posted Feb 19, '14 at 11:19am

weirdlike

weirdlike

624 posts

Knight

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

 

Posted Feb 19, '14 at 11:23am

weirdlike

weirdlike

624 posts

Knight

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

 

Posted Feb 19, '14 at 11:01pm

mightybob

mightybob

353 posts

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!

 

Posted Feb 20, '14 at 1:02am

weirdlike

weirdlike

624 posts

Knight

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

 

Posted Feb 24, '14 at 6:26pm

mightybob

mightybob

353 posts

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

 

Posted Feb 24, '14 at 6:38pm

weirdlike

weirdlike

624 posts

Knight

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

 

Posted Feb 24, '14 at 9:58pm

mightybob

mightybob

353 posts

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.

 

Posted Feb 24, '14 at 11:48pm

weirdlike

weirdlike

624 posts

Knight

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

 

Posted Feb 25, '14 at 1:22am

weirdlike

weirdlike

624 posts

Knight

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

 
Reply to Grid System & Placing objects into the grid (as2)

You must be logged in to post a reply!