display shape placement

Hello, I have a home screen that shows about 6 boxes. Right now the 6 boxes are positioned vertically. I want the boxes to be positioned 3 on the left of the screen and 3 on the right. I can figure out the right formula to do this. So far i have this code:

for i = 1, #adNames do button1 = widget.newButton({ label = adNames[i], onEvent = handleButtonEvent, defaultFile = "assets/images/card.png", width = 100, height = 100, }) button1.x = display.screenOriginX + 52 button1.y = i\*(Y/2) + 60

and it looks like this:

[sharedmedia=core:attachments:5407]

I want it to look like this:

[sharedmedia=core:attachments:5408]

thanks for any help!

Hi @robertsthomasd,

Typically, the best way to do this is to set a variable for a “count” and the X position outside of the loop, and then when your count reaches a certain amount, change the variables so that the boxes move over to the next “column” and also reset to the top “row”. Something like this:

[lua]

local currentX = display.screenOriginX + 52

local count = 1

for i = 1, #adNames do

    button1 = widget.newButton({

        label = adNames[i],

        onEvent = handleButtonEvent,

        defaultFile = “assets/images/card.png”,

        width = 100,

        height = 100,

    })

    button1.x = currentX

    button1.y = count*(Y/2) + 60

    count = count + 1

    if i == 4 then

        count = 1

        currentX = currentX + 100

    end

end

[/lua]

Hope this helps,

Brent

Hi @robertsthomasd,

Typically, the best way to do this is to set a variable for a “count” and the X position outside of the loop, and then when your count reaches a certain amount, change the variables so that the boxes move over to the next “column” and also reset to the top “row”. Something like this:

[lua]

local currentX = display.screenOriginX + 52

local count = 1

for i = 1, #adNames do

    button1 = widget.newButton({

        label = adNames[i],

        onEvent = handleButtonEvent,

        defaultFile = “assets/images/card.png”,

        width = 100,

        height = 100,

    })

    button1.x = currentX

    button1.y = count*(Y/2) + 60

    count = count + 1

    if i == 4 then

        count = 1

        currentX = currentX + 100

    end

end

[/lua]

Hope this helps,

Brent