Possible Runtime scoping issue on an External module?

I have an external module that creates an object and then is called to oscillate the object along the y-axis while the game is running, and stop when paused.  The object making and “start” call for oscillation works fine, but the Runtime:removeEventListener(“enterFrame”, oscillate) isn’t making the oscillation stop.  If I have a scoping issue, I’m not seeing it.  Thanks.


  • Obj.lua

local M = {}

function M:new()

    local object = display.newImage(“image.png”)

        … other code for placement

    

    function object:oscillateStartStop(action)

        local self = self

        local function oscillate(action)

            if (ifStop == true) then Runtime:removeEventListener(“enterFrame”, oscillate) end

            local getTime = system.getTimer()

            self.y = ( self.y ) + ( math.sin( getTime * 0.0015 ) * 0.5 )

        end

    

        if action == “start” then

            Runtime:addEventListener(“enterFrame”, oscillate)

        elseif action == “stop”

            oscillate(true)

        end

    end

    return object

end

return M 


–main.lua

local thing = M:new()

thing:oscillateStartStop(“start”)

…pause the game later on

thing:oscillateStartStop(“stop”)

    – oscillation doesn’t stop though print functions state that functions are being called

I think your problem may be here:

        local function oscillate(action)             if (ifStop == true) then Runtime:removeEventListener("enterFrame", oscillate) end               local getTime = system.getTimer()             self.y = ( self.y ) + ( math.sin( getTime \* 0.0015 ) \* 0.5 )         end

Sorry Rob,  that is absolutely a typo error on my part when I was transcribing the needed parts for the post.

    

 local function oscillate(action)  --\>  local function oscillate(ifStop)

 I’ve also tried putting the removeEventListener outside to the function call. no luck

[lua]

local M = {}

function M:new()
    local object = display.newImage(“image.png”)
        … other code for placement

    function object:oscillateStartStop(action)
          local self = self

          local function oscillate(action)
                local getTime = system.getTimer()
                self.y = ( self.y ) + ( math.sin( getTime * 0.0015 ) * 0.5 )
          end 

          if action == “start” then
                 Runtime:addEventListener(“enterFrame”, oscillate)
          elseif action == “stop”
                 Runtime:removeEventListener(“enterFrame”, oscillate)
          end

    end

     return object

end

 

return M

[/lua]

I think your problem may be here:

        local function oscillate(action)             if (ifStop == true) then Runtime:removeEventListener("enterFrame", oscillate) end               local getTime = system.getTimer()             self.y = ( self.y ) + ( math.sin( getTime \* 0.0015 ) \* 0.5 )         end

Sorry Rob,  that is absolutely a typo error on my part when I was transcribing the needed parts for the post.

    

 local function oscillate(action)  --\>  local function oscillate(ifStop)

 I’ve also tried putting the removeEventListener outside to the function call. no luck

[lua]

local M = {}

function M:new()
    local object = display.newImage(“image.png”)
        … other code for placement

    function object:oscillateStartStop(action)
          local self = self

          local function oscillate(action)
                local getTime = system.getTimer()
                self.y = ( self.y ) + ( math.sin( getTime * 0.0015 ) * 0.5 )
          end 

          if action == “start” then
                 Runtime:addEventListener(“enterFrame”, oscillate)
          elseif action == “stop”
                 Runtime:removeEventListener(“enterFrame”, oscillate)
          end

    end

     return object

end

 

return M

[/lua]