[Resolved] imageGroup item toFront()?

Hello, I’m new to LUA and I’m trying to learn the Corona best practices. I have three images that I load into a group (backgrounds). Clicking on one of the three on screen buttons displays on of the three images in the group. Currently I’m reloading the image into the group to move it to the top of the stack for display.

function changeBG0()  
  
 bgGroup:insert( redBox )  
  
end  

… this seems lazy to me. I have 3 backgrounds (redBox, blueBox and greenBox) - can I target a group member by name and move them to the top of the stack for display? ie something like this :

bgGroup[redBox]:toFront()  

[does not work, of course]

… or is the insert method the “BEST” way to handle this?

Thanks! [import]uid: 16278 topic_id: 28189 reply_id: 328189[/import]

Close, you’d just used redBox:toFront() and it would go to the front of the display group.

Plug and play example for iPhone portrait;
[lua]local bg1 = display.newRect( 0, 0, 320, 480 )
bg1:setFillColor(255, 0, 0)

local bg2 = display.newRect( 0, 0, 320, 480 )
bg2:setFillColor(0, 0, 255)

local bg3 = display.newRect( 0, 0, 320, 480 )
bg3:setFillColor(255, 255, 0)

bgGroup = display.newGroup()

bgGroup:insert(bg1)
bgGroup:insert(bg2)
bgGroup:insert(bg3)

local btn1 = display.newRect( 20, 20, 40, 20 )
btn1:setFillColor(0,0,0)
btn1.bg = bg1

local btn2 = display.newRect( 80, 20, 40, 20 )
btn2:setFillColor(0,0,0)
btn2.bg = bg2

local btn3 = display.newRect( 140, 20, 40, 20 )
btn3:setFillColor(0,0,0)
btn3.bg = bg3

local function changeBg(event)
event.target.bg:toFront()
end
btn1:addEventListener(“tap”, changeBg)
btn2:addEventListener(“tap”, changeBg)
btn3:addEventListener(“tap”, changeBg)[/lua]

Hope this helps!

Peach :slight_smile: [import]uid: 52491 topic_id: 28189 reply_id: 113896[/import]

That did it.

Thanks! [import]uid: 16278 topic_id: 28189 reply_id: 113905[/import]

Not a problem :slight_smile: Good luck with your project!

PS - Just for future reference, it’s “Lua” not “LUA” because it isn’t an abbreviation. Some people can be a little funny about that, or so I’ve been warned :wink: [import]uid: 52491 topic_id: 28189 reply_id: 113919[/import]