Need little help with placing different objects in rows and cols


(Froom7) #1

Heya peeps,
I kinda need a little help for my game.
What I want to do is create a grid and fill it with cards. I want them to place it randomly in the field (in rows and cols) everytime you start a new game. For now I have hardcoded every objects position. Can someone tell me how I can approach this problem?

Here is a screenshot with what I mean:


(Martí Angelats i Ribera) #2

It’s not hard:

first you have to know how many rows and cols you want. Then you make an array of length rows*cols and fill it with the random cards. Then you can use this

for (int col:int = 0; col < cols; col++)
{
    for (int row:int = 0; row < rows; row++)
    {
        //here you got the number of col rows wich are the col*rows+row position of the array.
    }
}

(Jean) #3

Complementing his answer, when you get to add the cards, you use FP.rand(amountAvaiableCards) then make this calc to add each one:

tempX = offsetX + (imageWidth * col) + (borderX * col);
tempY = offsetY + (imageHeight * row) + (borderY * row);

Offset is how much each card is away from the screen, and the border is how much each card is separated from each other.