Not able to understand what's going

  1. It is documented.  They are called table listeners:  

https://docs.coronalabs.com/guide/events/detectEvents/index.html#function-and-table-listeners

 
 
2. What I’m doing is clearing the variable that points to the function as a bookkeeping thing for my code.
 
Elsewhere I check to be sure obj.enterFrame is present before removing the listener.
 
If I don’t do this, it will crash.
 
Ex: This would crash:
 

function obj.enterFrame(self) end Runtime:addEventListener( "enterFrame", obj ) local function cancelIt() Runtime:removeEventListener( "enterFrame", obj ) obj.enterFrame = nil end cancelIt() ... later cancelIt()-- CRASH

This logic prevents the issue:
 

function obj.enterFrame(self) end Runtime:addEventListener( "enterFrame", obj ) local function cancelIt() if( not obj.enterFrame ) then return end Runtime:removeEventListener( "enterFrame", obj ) obj.enterFrame = nil end cancelIt() ... later cancelIt()

@anaqim,

Here are the docs on table listeners:
https://docs.coronalabs.com/guide/events/detectEvents/index.html#function-and-table-listeners

@swan…,

Correct me if I’m wrong but essentially this is what you want:

  • Track coal as counter
  • A ‘ball’ that moves automatically from left to right, except in certain cases (below)
  • Allow the user to click the ‘back’ button to increment coal counter
  • When the coal counter is incremented and found to be a multiple of 2, you want to stop the ball from moving for one second.
  • At the end of a one-second stop you want the ball to resume moving.

If that is correct, this is how I would do it:

local getTimer = system.getTimer local isAndroid = (system.getInfo("platform") == "android") local coal = 0 local pauseDuration = 1000 local ball = display.newCircle( 100, 100, 50 ) ball.\_waitTill = getTimer() - pauseDuration -- Ensure it can move immediately function ball.finalize( self ) Runtime:removeEventListener( "enterFrame", self ) end; ball:addEventListener( "finalize" ) function ball.enterFrame( self ) if( getTimer() \< self.\_waitTill ) then return end -- self.x = self.x + 1 end;Runtime:addEventListener( "enterFrame", ball ) if( isAndroid ) then ball.key = function( self, event ) if( event.keyName == "back" and event.phase=="up" ) then coal = coal + 1 if( coal % 2 ~= 0 ) then self.\_waitTill = getTimer() + pauseDuration end end return true end; Runtime:addEventListener( "key", ball ) end

Thanks for the info RG

I’ll be studying it  :slight_smile:

Sir @roaminggamer,

Your first three steps are correct I am correcting your 4th step

  • If counter is a multiple of two, ball should resume after one second and if its not a multiple of two then it should stop.
  • On the first click it should stop and on the second click it should start moving after one second.

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/04/coalBall.zip

local getTimer = system.getTimer local isAndroid = (system.getInfo("platform") == "android") local debugEn = true local coal = 0 local pauseDuration = 1000 local ball = display.newCircle( 100, 100, 50 ) ball.\_waitTill = getTimer() - pauseDuration -- Ensure it can move immediately ball.isMoving = true ball.allowResume = false function ball.finalize( self ) Runtime:removeEventListener( "enterFrame", self ) end; ball:addEventListener( "finalize" ) function ball.enterFrame( self ) print( self.isMoving, self.allowResume ) if( self.isMoving == false ) then if( self.allowResume == false ) then return false end if( getTimer() \< self.\_waitTill ) then return false end end -- self.x = self.x + 1 end; Runtime:addEventListener("enterFrame", ball) local coalLabel = display.newText( coal, 300, 300 ) if( isAndroid or debugEn ) then ball.key = function( self, event ) for k,v in pairs(event) do print(k,v) end if( event.phase=="up" and ( event.keyName == "back" or event.keyName == "space") ) then coal = coal + 1 coalLabel.text = coal .. " : " .. tostring(coal % 2) if( coal % 2 == 0 ) then self.isMoving = false self.allowResume = true self.\_waitTill = getTimer() + pauseDuration else self.isMoving = false self.allowResume = false end end return true end; Runtime:addEventListener( "key", ball ) end

Sir @roaminggamer,

Lots of thanks for this solution, it worked well. It has also taught some really helpful concepts which will surely help me in future. Thanks once again.

Regards,

Swanand Thakur