Currently I’m having a main object randomly “weaving” between parallax background objects when I tap a button. I figured the easiest way to give the illusion would be to have the main object move up and down the group index list.
Problem: the main object will move down the index list ( [3] -> [2] -> [1] ) but not up ( [1] -> [2] -> [3], etc)
Any help on my error below would be appreciated:
local btnTouch, enableBtn local group = display.newGroup() local bg1 = display.newGroup() bg1.name = "bg1" local bg2 = display.newGroup() bg1.name = "bg2" local bg3 = display.newGroup() bg1.name = "bg3" local mainObj = display.newCircl( 0, 0, 30 ) mainObj.name = "mainObj" mainObj.x = display.contentWidth / 2 mainObj.y = display.contentHeight / 2 group:insert( bg1 ) group:insert( bg2 ) group:insert( mainObj ) group:insert( bg3 ) local button = widget.newButton{ left = display.contentWidth - 200, top = display.contentHeight - 150, width = 100, height = 100, id = "btn", label = "Strafe", onPress = btnTouch } function enableBtn () button:setEnabled( true ) end function btnTouch( event ) local ran = math.random() local objPosition for i=1, group.numChildren do local name = group[i].name print( "group children "..i ..group[i].name ) if name == "mainObj" then objPosition = i end end -- set limits of mainObj's position within the group if objPosition == 1 then ran = 0.6 elseif objPosition == ( group.numChildren - 1 ) then ran = 0.3 end if ( "began" == event.phase) then button:setEnabled( false ) if ran \> 0.5 then objPosition = objPosition + 1 group:insert( objPosition, mainObj ) elseif ran \<= 0.5 then objPosition = objPosition - 1 group:insert( objPosition, mainObj ) end end return true end