AddEventListener: Listener cannot be nil

local function foo(event)

if … then
Runtime:removeEventListener(“enterFrame”, foo)
Runtime:addEventListener(“enterFrame”, bar)
end
end

local function bar(event)

If … then
Runtime:removeEventListener(“enterFrame”, bar)
Runtime:addEventListener(“enterFrame”, foo)
end
end

Runtime:addEventListener(“enterFrame”, bar)

-----> AddEventListener: Listener cannot be nil…

Are there any patterns to solve this problem?

No idea why you would want that pattern but try this

local bar, foo = nil, nil local function foo(event)   if ... then     Runtime:removeEventListener("enterFrame", foo)     Runtime:addEventListener("enterFrame", bar)   end end local function bar(event)   If ... then     Runtime:removeEventListener("enterFrame", bar)     Runtime:addEventListener("enterFrame", foo)   end end Runtime:addEventListener("enterFrame", bar)

No idea why you would want that pattern but try this

local bar, foo = nil, nil local function foo(event)   if ... then     Runtime:removeEventListener("enterFrame", foo)     Runtime:addEventListener("enterFrame", bar)   end end local function bar(event)   If ... then     Runtime:removeEventListener("enterFrame", bar)     Runtime:addEventListener("enterFrame", foo)   end end Runtime:addEventListener("enterFrame", bar)