Functions or Add/remove eventlisenter problems

Here’s my problem. And I might be going about this the wrong way, but I’m trying to enable/disable buttons.

To simplify things, I have 4 buttons. When one button is activated, it moves to the center of the screen, then needs to deactivate all other buttons, so only that button can be pressed and moved back to the original position. Now, I have this semi working, but what is happening, is after I enable the button, and that button should then disable the other buttons again, it doesn’t work. (To disable the button, i’m removing the event listener)

Here’s the basic code that you need to see.
–Cross, Trust, God, Man are all buttons. dragging is the function that brings the button to the center.-

local function DisableAll (event)
Cross:removeEventListener(“touch”, dragging)
Trust:removeEventListener(“touch”, dragging)
God:removeEventListener(“touch”, dragging2)
Man:removeEventListener(“touch”, dragging2)
end

local function EnableAll (event)
Cross:addEventListener(“touch”, dragging)
Trust:addEventListener(“touch”, dragging)
God:addEventListener(“touch”, dragging2)
Man:addEventListener(“touch”, dragging2)
end

–Here’s the function for the Cross button. All others are pretty much the same. Names corresponding to themselves of course.–
local function snapCross (event)
–go back to original state
if Cross.x < 130 and CrossIn == true then
EnableAll()
transition.to(Cross, {time=750, x=-156, delay=0, alpha=.6,transition=easing.outExpo})
timer.performWithDelay(750, CrossInFalse)
end
–go to center and CrossInTrue
if Cross.x > -110 and CrossIn == false then
DisableAll()
transition.to(Cross, {time=750, x=175, delay=0, alpha=1, transition=easing.outExpo, onComplete = Cross:addEventListener(“touch”, dragging)})
timer.performWithDelay(750, CrossInTrue)

end
–limit x from going past center
if Cross.x > 176 and CrossIn == true then
transition.to(Cross, {time=10, x=175, delay=0, alpha=1, transition=easing.outExpo})

end

end

So in Theory, every time snapCross if fired, it should disable all the buttons. And it does the first time, but after it gets Enabled again, then snapCross is fired again, it doesn’t work. Is there something that happens where functions only fire one time, then they are done? Is it perhaps an add/remove event listener problem? I’ve been wrestling with this for a couple days. It makes sense to me that it should work. Any help would be super appreciated. [import]uid: 11144 topic_id: 6230 reply_id: 306230[/import]

Is there something that happens where functions only fire one time, then they are done? Is it perhaps an add/remove event listener problem?

Well the answer to the first question is no, so there is definitely a problem with how you are adding and removing event listeners. It’s a little hard to follow what’s happening in your code (use < code > tags and indent next time) but just skimming through the code it looks like you end up setting two “touch” listeners on Cross, and so that may be causing one listener to call EnableAll() immediately after the other calls DisableAll(). I would suggest your enable/disable functions should take a parameter for one button not to enable or disable in order to make the logic a little clearer about which buttons have event listeners set, which have the event listener removed, and in both cases when.

If you haven’t already been doing so, the way to test for this sort of thing is to put print() statements in your code and look at the output. For example, put print(“here I am”) in the snapCross() function and then pay attention to how many times you see the message output to the terminal. [import]uid: 12108 topic_id: 6230 reply_id: 21412[/import]

Good call on using the print function. I figured out part of the problem. You were right, it kept calling the function over and over again because I had two event listeners going off and conflicting against each other for the same button. So, I’m working through it.

Question that might solve another issue that’s coming up related to it. So, if i want to call the disable function when a button/image passes a certain coordinate, how would I go about doing that. Example would be if

MyImage passes past y=300, then call function DisableButtons

does that make any sense? Maybe I’m going about this all wrong. Thanks for taking the time to help a newbe. [import]uid: 11144 topic_id: 6230 reply_id: 21441[/import]

check the position of myImage every frame.
This might be the same function that is moving myImage in each frame.
[lua]local function moveDisable ( event )
if myImage.x > xMax then – xMax being a global in this example
disableButtons()
end
end – moveDisable
myImage.enterFrame = moveDisable
Runtime:addEventListener(“enterFrame” ,myImage )[/lua] [import]uid: 12635 topic_id: 6230 reply_id: 21524[/import]