Re-assign event listener (RESOLVED)

In my game, I launch a marble towards a group of glass containers, trying to land one inside. The current marble has a touch listener assigned to it when it is created and ready to launch:

curMarble = display.newImage("images/marble.png")  
curMarble:addEventListener("touch", marbleTouchListener)  

As soon as the marble is launched, I remove the listener.

curMarble:removeEventListener("touch", marbleTouchListener)  

When the marble comes to rest, another one is created and assigned to curMarble, and the touch listener is again added. i.e., the first code snippet is again executed.

However, this time it doesn’t respond to touch events, and neither does the original one. Any ideas what happened to the event listener? [import]uid: 58455 topic_id: 11747 reply_id: 311747[/import]

Can you post the whole code you have for creating the marble? Also make sure whatever you have for marbleTouchListener is above your code to create the marbles. [import]uid: 69700 topic_id: 11747 reply_id: 42766[/import]

The only thing I didn’t show was the group insert. And yes, the listener is above the code that uses it. It works for the first marble, but not the second.

curMarble = display.newImage("images/marble.png") gameGroup:insert(curMarble) curMarble:addEventListener("touch", marbleTouchListener) [import]uid: 58455 topic_id: 11747 reply_id: 42771[/import]

You said as soon as it comes to rest another one is created. Where is the part of coding you have for that? [import]uid: 69700 topic_id: 11747 reply_id: 42776[/import]

You said as soon as it comes to rest another one is created. Where is the part of coding you have for that? [import]uid: 69700 topic_id: 11747 reply_id: 42777[/import]

That’s in a RuntimeEventListener on enterFrame:

function gameLoop(e)  
 if (curMarble == nil) then  
 newMarble()  
 else  
 local vx, vy = curMarble:getLinearVelocity()  
 if (math.abs(vx) \< 10 and math.abs(vy) \< 10 and slingshotEmpty) then  
 newMarble()  
 end  
 end  
end  

newMarble() is the function in the previous post. It gets called, because the marble is created. It just doesn’t respond to touch events.
[import]uid: 58455 topic_id: 11747 reply_id: 42778[/import]

I think this might be a bug in removeEventListener. I added a new object reference, nextMarble, that was created and assigned the touch listener after curMarble came to a rest. Same problem. No touch listener on either marble. [import]uid: 58455 topic_id: 11747 reply_id: 42840[/import]

Resolved. Another brain dead programming error by me. I was setting a condition in the touch handler that wasn’t get reset when the new marble was generated.

[import]uid: 58455 topic_id: 11747 reply_id: 42859[/import]