Adding addEventListener crashes the simulator

In my game, I create items, add them to a displayGroup then add tap eventListeners to them. When an item is clicked, I remove the listeners for the duration of my code loop, then add them back in (basically disable touches to any objects while I am executing code).

Everything works until I try to add the eventListeners back in. It crashes the simulator and an actual device. I am using v.268

[code]
–I’ve removed unnecessary code
local DGroup = display.newGroup()

–Method that gets called from eventListener
local removeBox = function( event )
local originalBoxPassedIn = event.target
findBoxesToRemove(originalBoxPassedIn)
end

–Initial setup of boxes on screen (70 boxes created)
boxesMatrix = {}
for i=1,10 do
boxesMatrix[i] = {}

for j=1,7 do
boxesMatrix[i][j] = {}

DisplayNum = DisplayNum + 1
boxesMatrix[i][j].image = display.newImage( “buttonImage”…choice…“s.png” )
boxesMatrix[i][j].image.myIndex = DisplayNum
DGroup:insert( boxesMatrix[i][j].image )
DGroup[DisplayNum]:addEventListener( “tap”, removeBox )
end
end

function findBoxesToRemove(originalBoxPassedIn)
–This code works, it makes all the boxes non-clickable
for x=1,DGroup.numChildren do
DGroup[x]:removeEventListener( “tap”, removeBox )
end

–Here is my long code loop
–…

–After the loop, try to add all the listeners back in
for y=1,DGroup.numChildren do
DGroup[y]:addEventListener( “tap”, removeBox )
–Calling this causes the simulator to crash
end
end

[/code] [import]uid: 28218 topic_id: 6501 reply_id: 306501[/import]

I was not able to make an index of a displaygroup so maybe you will have to add your tap eventlistener like

Runtime:addEventListener(“tap”,removeBox)

then it will be application wide event but will not crash :slight_smile:
[import]uid: 22737 topic_id: 6501 reply_id: 22547[/import]

Thanks for the suggestion. I’m not sure adding a runtime listener will work because it doesn’t target a specific box, only the entire display if I understand it right.

I moved my re-add listeners code above the removeBox function and managed to get some more detailed error info:

Runtime error
assertion failed!
stack traceback:
[C]: ?
[C]: in function ‘assert’
?: in function ‘getOrCreateTable’
?: in function ‘addEventListener’
/Users/Ryan/Desktop/Boxes8/main.lua:30: in function ‘addListenersBackIn’
/Users/Ryan/Desktop/Boxes8/main.lua:183: in function ‘findBoxesToRemove’
/Users/Ryan/Desktop/Boxes8/main.lua:44: in function
?: in function <?:214>

Any ideas please??
It is very frustrating that I can add a listener, remove it and then try to add it back on the exact same way it was created except that it’s in a different place in the code and it breaks. [import]uid: 28218 topic_id: 6501 reply_id: 22648[/import]

Hello, you can use a local boolean variable for control:

[lua]–I’ve removed unnecessary code
local DGroup = display.newGroup()
local tapEnabled = true – Here

–Method that gets called from eventListener
local removeBox = function( event )
if tapEnabled then – Here
tapEnabled = false – Here
local originalBoxPassedIn = event.target
findBoxesToRemove(originalBoxPassedIn)
end – Here
end

–Initial setup of boxes on screen (70 boxes created)
boxesMatrix = {}
for i=1,10 do
boxesMatrix[i] = {}

for j=1,7 do
boxesMatrix[i][j] = {}

DisplayNum = DisplayNum + 1
boxesMatrix[i][j].image = display.newImage( “buttonImage”…choice…“s.png” )
boxesMatrix[i][j].image.myIndex = DisplayNum
DGroup:insert( boxesMatrix[i][j].image )
DGroup[DisplayNum]:addEventListener( “tap”, removeBox )
end
end

function findBoxesToRemove(originalBoxPassedIn)
–This code works, it makes all the boxes non-clickable
– for x=1,DGroup.numChildren do – Here
– DGroup[x]:removeEventListener( “tap”, removeBox ) – Here
– end – Here

–Here is my long code loop
–…

–After the loop, try to add all the listeners back in
– for y=1,DGroup.numChildren do – Here
– DGroup[y]:addEventListener( “tap”, removeBox ) – Here
–Calling this causes the simulator to crash
– end – Here
tapEnabled = true – Here
end[/lua]

I marked with “-- Here” the lines affected. I hope this help you.

Regards.
Francisco. [import]uid: 11749 topic_id: 6501 reply_id: 22673[/import]

Thank you very much for your reply. I used your idea and added in a performWithDelay to allow enough time for my transitions to execute and finish running all the code before setting tapEnabled = true. [import]uid: 28218 topic_id: 6501 reply_id: 22879[/import]