[Solved] Display groups

Hi,

I’m creating 2 displaygroups, 1 with a background, then another with a button.

When displayed, the button(group) is always displayed UNDER the background group, even when i load the backgroundgroup first.

I tried bgGroup:toBack() without result.

Funny thing is, when i want to drag the background around, and use t:toBack() in the startDrag function (line 36), the background group is set back properly, and the button is now on top of the background.

What am i doing wrong?

[code]
module(…, package.seeall)

new = function ( params )


– Display Objects

bgGroup = display.newGroup()
background = display.newImage(“images/common/locations/background.jpg”, 0,0 )
bgGroup:insert( background )

mainGroup = display.newGroup()
button = display.newImage(“images/common/btnBack.png”, 0,0 )
button.x = 64
button.y = 716
mainGroup:insert( button )

bgGroup:toBack()

local function startDrag(event )

local t = event.target
local phase = event.phase

if “began” == phase then

Dragging = false
display.getCurrentStage():setFocus( t )
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

– t:toBack()

elseif t.isFocus then

if “moved” == phase then
Dragging = true

t.x = event.x - t.x0
t.y = event.y - t.y0

if t.x > 0 then
t.x = 0
end
if t.x < -1024 then
t.x = -1024
end
if t.y < -768 then
–t.y = -768
end
if t.y > 0 then
t.y = 0
end

elseif “ended” == phase or “cancelled” == phase then

display.getCurrentStage():setFocus( nil )
t.isFocus = false

end
end

– Stop further propagation of touch event!
return true
end

local initVars = function ()


– Inserts

bgGroup:addEventListener( “touch”, startDrag )
bgGroup:toBack()

end


– Initiate variables

initVars()


– MUST return a display.newGroup()

return mainGroup

end
[/code] [import]uid: 50459 topic_id: 25634 reply_id: 325634[/import]

You are creating mainGroup = display.newGroup() twice. Remove line 9 [import]uid: 7177 topic_id: 25634 reply_id: 103581[/import]

Yes saw that, even with that correction it doesn’t work [import]uid: 50459 topic_id: 25634 reply_id: 103582[/import]

I see your “return mainGroup” line 95 where does this get return too and what happens with it? [import]uid: 7177 topic_id: 25634 reply_id: 103585[/import]

I’m using director 1.4, and you need to return the localgroup… [import]uid: 50459 topic_id: 25634 reply_id: 103587[/import]

if that’s the case then you need to return one group that contains all your separate elements.

mainGroup:insert( bgGroup)

or

coreGroup = display.newGroup()
coreGroup:insert(bgGroup )
coreGroup:insert(mainGroup)

and return coreGroup [import]uid: 7177 topic_id: 25634 reply_id: 103593[/import]

That did the trick! Thank you!

Learn something new everyday :slight_smile:

[import]uid: 50459 topic_id: 25634 reply_id: 103595[/import]