Group issue

Hi,

I’m studing the “Groups” in Corona but I have  a trouble:

when I try to repeat two times my program Corona goes in error with this window:

File: …Lab7/iPhone/Corona SDK/Sviluppo/Test/Groups/main.lua

Line: 19

ERROR: Attempt to remove an object that’s already been removed from the stage or whose parent/ancestor group has already been removed.

Can you help me?

This is my program:

cubo3= display.newRect(100, 500, 180, 200) 

function build()

    cubo= display.newRect(30, 100, 180, 200) 

    cubo:setFillColor(00, 140, 250)

    cubo2= display.newRect(100, 250, 200, 120) 

    cubo2:setFillColor(100, 140, 00)

    testo=display.newText(“TEXT”,40,250,native.systemFont, 22)

    livello1=display.newGroup() – Generate new group

    livello1:insert(cubo)  – add elements to group

    livello1:insert(cubo2)

    livello1:insert(testo)

    testo:toFront()  – text to front

    livello1:addEventListener(“touch”, erase)

end

function erase()  – erase the group

    print(“Group”)

    livello1:removeSelf()

end

build()    – launch the build function

cubo3:addEventListener(“touch”, build)  – inf cubo3 is pressed launch function build to restart

Thank’s.

Andrea

Hi Andrea.  Touch events have multiple phases.  Your erase function will get called at least 2 times for every touch and release.

Try this:

function erase( event )  – erase the group

if event.phase == “ended” then

print(“Group”)

livello1:removeSelf()

livello = nil

end

return true

end

I bolded my additions.

Thank you Rob,

I have tryed your mod but don’t work well.

If I touch the blue and green box for the first time, they disappear (all ok),

then I touch the white box and they will be redraw. But if I retouch blue and green box to repeat the function, the first time don’t happen nothing and the second time, the Corona simulator stop to work and write me this message:

Corona Simulator Runtime error

File: …Lab7/iPhone/Corona SDK/Sviluppo/Test/Groups/main.lua

Line: 20

Attempt to index global ‘livello1’ (a nil value)

stack traceback:

    [C]: ?

    …Lab7/iPhone/Corona SDK/Sviluppo/Test/Groups/main.lua:20: in function <…Lab7/iPhone/Corona SDK/Sviluppo/Test/Groups/main.lua:17>

    ?: in function <?:218>

 

 

 

Have you any idea?

 

This is my actual list:

 

 

 

cubo3= display.newRect(100, 500, 180, 200) 

function build()

    cubo= display.newRect(30, 100, 180, 200) 

    cubo:setFillColor(00, 140, 250)

    cubo2= display.newRect(100, 250, 200, 120) 

    cubo2:setFillColor(100, 140, 00)

    testo=display.newText(“TEXT”,40,250,native.systemFont, 22)

    livello1=display.newGroup() – Generate new group

    livello1:insert(cubo)  – add elements to group

    livello1:insert(cubo2)

    livello1:insert(testo)

    testo:toFront()  – text to front

    livello1:addEventListener(“touch”, erase)

end

function erase( event )  – erase the group

    if event.phase == “ended” then

        print(“Group”)

        livello1:removeSelf()

        livello1 = nil

    end

    return true

end

build()    – launch the build function

cubo3:addEventListener(“touch”, build)

 

 

 

Thank’s

Andrea 

cubo3= display.newRect(100, 500, 180, 200) function build() &nbsp;&nbsp;&nbsp; cubo= display.newRect(30, 100, 180, 200) &nbsp;&nbsp;&nbsp; cubo:setFillColor(00, 140, 250) &nbsp;&nbsp;&nbsp; cubo2= display.newRect(100, 250, 200, 120) &nbsp;&nbsp;&nbsp; cubo2:setFillColor(100, 140, 00) &nbsp;&nbsp;&nbsp; testo=display.newText("TEXT",40,250,native.systemFont, 22) &nbsp;&nbsp;&nbsp; livello1=display.newGroup() -- Generate new group &nbsp;&nbsp;&nbsp; livello1:insert(cubo)&nbsp; -- add elements to group &nbsp;&nbsp;&nbsp; livello1:insert(cubo2) &nbsp;&nbsp;&nbsp; livello1:insert(testo) &nbsp;&nbsp;&nbsp; testo:toFront()&nbsp; -- text to front &nbsp;&nbsp;&nbsp; livello1:addEventListener("touch", erase) end function erase()&nbsp; -- erase the group &nbsp;&nbsp;&nbsp; print("Group") &nbsp;&nbsp;&nbsp; livello1:removeSelf() &nbsp;&nbsp;&nbsp; livello1 = nil end build()&nbsp;&nbsp;&nbsp; -- launch the build function local function buildOnTouch(event) &nbsp;&nbsp;&nbsp;&nbsp; if event.phase == "ended" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; build() &nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp; return true end cubo3:addEventListener("touch", buildOnTouch)&nbsp; -- inf cubo3 is pressed launch function build to restart

The problem is since the “build” function is also being called on touch, it was running twice causing problems.  Because touch events need a parameter passed, I wrote a function to handle the touch that calls your build function instead of the touch trying to call it directly.  It will work directly,  but you have to do some non-standard stuff to make your first call to build work.  This is a bit more “normal”.

Thank you very much!

Now is working very well!

Bye

Andrea

Hi Andrea.  Touch events have multiple phases.  Your erase function will get called at least 2 times for every touch and release.

Try this:

function erase( event )  – erase the group

if event.phase == “ended” then

print(“Group”)

livello1:removeSelf()

livello = nil

end

return true

end

I bolded my additions.

Thank you Rob,

I have tryed your mod but don’t work well.

If I touch the blue and green box for the first time, they disappear (all ok),

then I touch the white box and they will be redraw. But if I retouch blue and green box to repeat the function, the first time don’t happen nothing and the second time, the Corona simulator stop to work and write me this message:

Corona Simulator Runtime error

File: …Lab7/iPhone/Corona SDK/Sviluppo/Test/Groups/main.lua

Line: 20

Attempt to index global ‘livello1’ (a nil value)

stack traceback:

    [C]: ?

    …Lab7/iPhone/Corona SDK/Sviluppo/Test/Groups/main.lua:20: in function <…Lab7/iPhone/Corona SDK/Sviluppo/Test/Groups/main.lua:17>

    ?: in function <?:218>

 

 

 

Have you any idea?

 

This is my actual list:

 

 

 

cubo3= display.newRect(100, 500, 180, 200) 

function build()

    cubo= display.newRect(30, 100, 180, 200) 

    cubo:setFillColor(00, 140, 250)

    cubo2= display.newRect(100, 250, 200, 120) 

    cubo2:setFillColor(100, 140, 00)

    testo=display.newText(“TEXT”,40,250,native.systemFont, 22)

    livello1=display.newGroup() – Generate new group

    livello1:insert(cubo)  – add elements to group

    livello1:insert(cubo2)

    livello1:insert(testo)

    testo:toFront()  – text to front

    livello1:addEventListener(“touch”, erase)

end

function erase( event )  – erase the group

    if event.phase == “ended” then

        print(“Group”)

        livello1:removeSelf()

        livello1 = nil

    end

    return true

end

build()    – launch the build function

cubo3:addEventListener(“touch”, build)

 

 

 

Thank’s

Andrea 

cubo3= display.newRect(100, 500, 180, 200) function build() &nbsp;&nbsp;&nbsp; cubo= display.newRect(30, 100, 180, 200) &nbsp;&nbsp;&nbsp; cubo:setFillColor(00, 140, 250) &nbsp;&nbsp;&nbsp; cubo2= display.newRect(100, 250, 200, 120) &nbsp;&nbsp;&nbsp; cubo2:setFillColor(100, 140, 00) &nbsp;&nbsp;&nbsp; testo=display.newText("TEXT",40,250,native.systemFont, 22) &nbsp;&nbsp;&nbsp; livello1=display.newGroup() -- Generate new group &nbsp;&nbsp;&nbsp; livello1:insert(cubo)&nbsp; -- add elements to group &nbsp;&nbsp;&nbsp; livello1:insert(cubo2) &nbsp;&nbsp;&nbsp; livello1:insert(testo) &nbsp;&nbsp;&nbsp; testo:toFront()&nbsp; -- text to front &nbsp;&nbsp;&nbsp; livello1:addEventListener("touch", erase) end function erase()&nbsp; -- erase the group &nbsp;&nbsp;&nbsp; print("Group") &nbsp;&nbsp;&nbsp; livello1:removeSelf() &nbsp;&nbsp;&nbsp; livello1 = nil end build()&nbsp;&nbsp;&nbsp; -- launch the build function local function buildOnTouch(event) &nbsp;&nbsp;&nbsp;&nbsp; if event.phase == "ended" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; build() &nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp; return true end cubo3:addEventListener("touch", buildOnTouch)&nbsp; -- inf cubo3 is pressed launch function build to restart

The problem is since the “build” function is also being called on touch, it was running twice causing problems.  Because touch events need a parameter passed, I wrote a function to handle the touch that calls your build function instead of the touch trying to call it directly.  It will work directly,  but you have to do some non-standard stuff to make your first call to build work.  This is a bit more “normal”.

Thank you very much!

Now is working very well!

Bye

Andrea