Attempt to call method 'removeEventListener' (a nil value)

I have many groups on the screen [1-30] and some buttons at the end of the screen.

Depending on button clicks alternations groups get removed and repositioned.

At the end 3 groups remains on the screen (it can be anyone from 1-30 depending on buttons click alternation).

Whatever group is clicked it gets removed from the screen and the other 2 repositioned again. On the last 2 ones (both remain on screen): whichever gets clicked gets resized smaller and the other one bigger.

For 3 remaining groups I call an addEventListener for each group, but it ends up removing all them on clicking over. I need to removeEventListener for 2 remaining groups when clicking first group from 3 left and then add another EventListener to each of them to resize.

I am having trouble solving this situation.

Keeps returning “Attempt to call method ‘removeEventListener’ (a nil value)”

Do you have any idea please?

How can I achieve this performance?

Here is some of my code:

local function 3groupsremaining(group1,group2,group3) return function(event) groupList[group1]:removeSelf() transition.to(groupList[group2], {time=1000, x=0, y=0}) transition.to(groupList[group3], {time=1000, x=50, y=0}) groupList[group2]:removeEventListener( "touch", 3groupsremaining(1,2,3)) groupList[group3]:removeEventListener( "touch", 3groupsremaining(1,2,3)) return true end end groupList[1]:addEventListener("touch", 3groupsremaining(1, 2, 3)) groupList[2]:addEventListener("touch", 3groupsremaining(2, 1, 3)) groupList[3]:addEventListener("touch", 3groupsremaining(3, 1, 2))

removeEventListener (nil value) comes because i call an addEventListener per each group;

that eventListener attempts to remove that group from the screen so it comes as nil when i try to removeEventListener.

But i don’t know what group user will click, so need to get track of all possibilities. 

I don’t think it’s possible to pass in a parameter list to an event listener using Corona.

For your specific question, the workaround shouldn’t be too difficult.

local groupList = {group1, group2, group2} local function 3groupsremaining(event) --Remove group that was touched for i = 1, #groupList do if (groupList[i] == event.target) then groupList[i]:removeSelf() table.remove(groupList, i) end end --Shift other remaining groups over transition.to(groupList[0], {time=1000, x=0, y=0}) transition.to(groupList[1], {time=1000, x=50, y=0}) return true end groupList[1]:addEventListener("touch", 3groupsremaining) groupList[2]:addEventListener("touch", 3groupsremaining) groupList[3]:addEventListener("touch", 3groupsremaining)

Hi @atrizhong,

Thank you so much for your reply and help.

Just implemented your solution, but the result is still the same.

Onclick of each button they get removed even there 2 left on screen.

i think the problem is on adding EventListener per each group, because if i comment 2 last lines, i can get the result

(but only for one group)

Do you have any idea please?

I have no idea what’s wrong. Can you post a working sample of your code that I can test and run?

Project is complicated and i can’t take a part of it as a sample , because of many conditions happening. 
imagine there are 30 groups on the screen (lets say 30 square images with a text number on top that varies from 1-30)

After some time and certain conditions we are at the stage where 3 groups are left on the screen.

Important key: Each one of the 3 remaining groups can be from 1-30 (depending on the conditions how they got eliminated)

OnClicking over on one of the 3 groups: remove the clicked one, resize second clicked, resize last left.

Is it more clear @atrizhong? Let me know your thoughts please :slight_smile:

To Simplify it (think it this way):

1- If i have 30 groups on the screen,

2- addEventListener to each of the groups

3- user clicks one of the groups (we dont know which one he clicks from 30 choices)

4- RemoveEventListerners for all other 29 groups ?!

This is want i want to achieve RemoveEventListners for all the others.

But as corona code gets executed line by line and we had added 30 addEventListeners row by row, 

it recognizes the eventlistener that deletes the group

Ok, I think I know what you’re asking now. After removing the first square, you want to disable touch events for the rest of the squares.

Gotta call:

groupList[i]:removeEventListener(“touch”, 3groupsremaining)

for each one of the remaining squares

local groupList = {group1, group2, group2} local function 3groupsremaining(event) --Remove group that was touched for i = 1, #groupList do if (groupList[i] == event.target) then groupList[i]:removeSelf() table.remove(groupList, i) end end --Disable touch for other groups for i = 1, #groupList do groupList[i]:removeEventListener("touch", 3groupsremaining) end --Shift other remaining groups over transition.to(groupList[0], {time=1000, x=0, y=0}) transition.to(groupList[1], {time=1000, x=50, y=0}) return true end groupList[1]:addEventListener("touch", 3groupsremaining) groupList[2]:addEventListener("touch", 3groupsremaining) groupList[3]:addEventListener("touch", 3groupsremaining)

Exactly, i did a small module separately and worked same logic as above :),
but still in my project does not work. I need to figure out where the problem stand now

removeEventListener (nil value) comes because i call an addEventListener per each group;

that eventListener attempts to remove that group from the screen so it comes as nil when i try to removeEventListener.

But i don’t know what group user will click, so need to get track of all possibilities. 

I don’t think it’s possible to pass in a parameter list to an event listener using Corona.

For your specific question, the workaround shouldn’t be too difficult.

local groupList = {group1, group2, group2} local function 3groupsremaining(event) --Remove group that was touched for i = 1, #groupList do if (groupList[i] == event.target) then groupList[i]:removeSelf() table.remove(groupList, i) end end --Shift other remaining groups over transition.to(groupList[0], {time=1000, x=0, y=0}) transition.to(groupList[1], {time=1000, x=50, y=0}) return true end groupList[1]:addEventListener("touch", 3groupsremaining) groupList[2]:addEventListener("touch", 3groupsremaining) groupList[3]:addEventListener("touch", 3groupsremaining)

Hi @atrizhong,

Thank you so much for your reply and help.

Just implemented your solution, but the result is still the same.

Onclick of each button they get removed even there 2 left on screen.

i think the problem is on adding EventListener per each group, because if i comment 2 last lines, i can get the result

(but only for one group)

Do you have any idea please?

I have no idea what’s wrong. Can you post a working sample of your code that I can test and run?

Project is complicated and i can’t take a part of it as a sample , because of many conditions happening. 
imagine there are 30 groups on the screen (lets say 30 square images with a text number on top that varies from 1-30)

After some time and certain conditions we are at the stage where 3 groups are left on the screen.

Important key: Each one of the 3 remaining groups can be from 1-30 (depending on the conditions how they got eliminated)

OnClicking over on one of the 3 groups: remove the clicked one, resize second clicked, resize last left.

Is it more clear @atrizhong? Let me know your thoughts please :slight_smile:

To Simplify it (think it this way):

1- If i have 30 groups on the screen,

2- addEventListener to each of the groups

3- user clicks one of the groups (we dont know which one he clicks from 30 choices)

4- RemoveEventListerners for all other 29 groups ?!

This is want i want to achieve RemoveEventListners for all the others.

But as corona code gets executed line by line and we had added 30 addEventListeners row by row, 

it recognizes the eventlistener that deletes the group

Ok, I think I know what you’re asking now. After removing the first square, you want to disable touch events for the rest of the squares.

Gotta call:

groupList[i]:removeEventListener(“touch”, 3groupsremaining)

for each one of the remaining squares

local groupList = {group1, group2, group2} local function 3groupsremaining(event) --Remove group that was touched for i = 1, #groupList do if (groupList[i] == event.target) then groupList[i]:removeSelf() table.remove(groupList, i) end end --Disable touch for other groups for i = 1, #groupList do groupList[i]:removeEventListener("touch", 3groupsremaining) end --Shift other remaining groups over transition.to(groupList[0], {time=1000, x=0, y=0}) transition.to(groupList[1], {time=1000, x=50, y=0}) return true end groupList[1]:addEventListener("touch", 3groupsremaining) groupList[2]:addEventListener("touch", 3groupsremaining) groupList[3]:addEventListener("touch", 3groupsremaining)

Exactly, i did a small module separately and worked same logic as above :),
but still in my project does not work. I need to figure out where the problem stand now