Problem with removeEventListener()/game loop

Hello all,

I am using director.lua to change scenes when options are selected from menu and that is working fine.
But now i try to include some animations for those menus.

in end of menu.lua (which is my main menu) i set up event listener

[lua]Runtime:addEventListener(“enterFrame”, menuAnimation)[/lua]

menuanimation in menu.lua
[lua]local function menuAnimation()
–>Here i will update my menu animation if possible
end[/lua]

and then i have pressPlay button event function in menu.lua
[lua]local function pressPlay (event)
if event.phase == “ended” then
–>This removeEventListener is my problem atm
Runtime:removeEventListener(“enterFrame”, menuAnimation)
director:changeScene (“game”, “moveFromRight”)
end
end[/lua]

First: Why that removeEventListener call don’t do what “i think it should do”? and where and how i need to call it so menuAnimation() calls on every frame end?

Second: Should I even do like this because I am planing to use this also how my gameloop work, or is there some better way to do it? Kind of need some information to get pushed in right direction with these game/animationloops.
[import]uid: 78572 topic_id: 13084 reply_id: 313084[/import]

I don’t quite understand what the removeEventListener call isn’t doing that you think it should. Are you running an animation that you want to stop when the player touches a button? [import]uid: 27965 topic_id: 13084 reply_id: 48074[/import]

the code looks ok. you said don’t do what you think it should do but din’t say what it does right now.

your code already does what it requires to call menuAnimation on each frame.
Runtime:addEventListener(“enterFrame”, menuAnimation)

are you getting any error ? whats the problem you are facing ? [import]uid: 71210 topic_id: 13084 reply_id: 48076[/import]

Yes, i set up eventlistener to call menuanimation function on start of every frame ( this keep animations running while in main menu) and when user push Play button from main menu, to play actual game, removeEventListener should stop those menuanimation funtion calls, but it don’t do it and those menuanimation calls keep going all the time. [import]uid: 78572 topic_id: 13084 reply_id: 48075[/import]

Solved this by changing this…
[lua]local function menuAnimation()

end[/lua]

to this…
[lua]function menuAnimation()

end[/lua]

And now it work as supposed: quit calling menuAnimations() when entering in different scene and begin call it again when entering back to menu scene. [import]uid: 78572 topic_id: 13084 reply_id: 48147[/import]

Same problem here, thank you so much for figuring it out. I have been working at this bug for ages now with no idea what could be wrong. Took off the word “local” and it magically works properly now!!! Thanks so much! [import]uid: 94309 topic_id: 13084 reply_id: 67164[/import]

Move your menuAnimation function above your press play function and it should solve the problem. You shouldn’t need to remove the local declaration. [import]uid: 84637 topic_id: 13084 reply_id: 67217[/import]