Timer question for Whack-a-mole

I am just getting back into Corona after about 5 years out of it.  I teach high school programming and figured Corona is a good platform to get kids into app programming.  I am just a bit rusty and the problem should be a simple answer.

I am trying to make a very simple whack-a-mole game before I give it to the kids to program.  Nine red buttons. One of them randomly selected turns blue.  Touch it before it turns back to red.  It is the timer for the life of the blue square that has me going.  The performWithDelay is not strictly a timer.  I just want a timer that if I tap the button, the timer will die and the button will turn red.  There is probably some clever way of massaging the performWithDelay to make it work.  

This is one way to implement such a whack-a-mole game:

Download here: http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/10/whack.zip

Code:

-- ============================================================= io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) -- ============================================================= -- BEGIN ANSWER -- ============================================================= -- Table to track moles local moles = {} -- Score Counter local score = 0 -- Score Label local scoreLabel = display.newText( "Score: 0", display.contentCenterX, display.contentCenterY - display.actualContentHeight/2 + 50, native.systemFontBold, 36 ) -- Handle for last timer local lastTimer -- Function to select a mole local function selectNewMole( subtractOne ) if (lastTimer) then timer.cancel( lastTimer ) end -- Player never tapped mole, take away a point if( subtractOne ) then score = score - 1 scoreLabel.text = "Score: " .. score end -- Clear all moles for i = 1, #moles do moles[i]:setFillColor( 1, 0, 0 ) moles[i].isBlue = false end -- Choose a random mole and make it blue local moleNum = math.random( 1, #moles ) moles[moleNum]:setFillColor( 0, 0, 1 ) moles[moleNum].isBlue = true -- Select a new mole in 1.5 seconds lastTimer = timer.performWithDelay( 1500, function() selectNewMole(true) end ) end -- Common touch listener for moles local touch = function( self, event ) if( event.phase == "began" ) then if( self.isBlue ) then -- Right mole, get a point self.isBlue = false score = score + 1 selectNewMole() else -- Wrong mole, lose a point score = score - 1 end end scoreLabel.text = "Score: " .. score return true end -- Create the 'moles' local moleSize = 120 local moleSpacing = 160 local startX = display.contentCenterX - moleSpacing local startY = display.contentCenterY - moleSpacing for i = 1, 3 do for j = 1, 3 do local mole = display.newRect( startX + (i-1) \* moleSpacing, startY + (j-1) \* moleSpacing, moleSize, moleSize) -- Start all moles as red mole:setFillColor( 1, 0, 0 ) -- Add common touch listener to this mole mole.touch = touch mole:addEventListener( "touch" ) -- Save the mole in our list moles[#moles+1] = mole end end selectNewMole() -- ============================================================= -- END ANSWER -- =============================================================

But this solution is so much more less complicated, less convoluted and less inefficient than my almost working solution.  Thank you.  Now I just have to figure out a couple lines of your code before I am happy.

You’re welcome.  Sorry to code dump like that, but it sounded fun, so I did it.  :unsure:

Paste the troublesome lines here with questions and I’ll see if I can clarify.

The code dump is great.  I will puzzle through the code for a while on my own.  I learn a lot just by the research.  I can tell I have been teaching beginning level programming for too long.  A parent is a CS prof at the local university.  I will  hit him up for answers.  He has been considering using Corona for his summer kid camps and this would be a good one to look at.  If we cannot figure it out I will drop you a note.  Thanks again.

This is one way to implement such a whack-a-mole game:

Download here: http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/10/whack.zip

Code:

-- ============================================================= io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) -- ============================================================= -- BEGIN ANSWER -- ============================================================= -- Table to track moles local moles = {} -- Score Counter local score = 0 -- Score Label local scoreLabel = display.newText( "Score: 0", display.contentCenterX, display.contentCenterY - display.actualContentHeight/2 + 50, native.systemFontBold, 36 ) -- Handle for last timer local lastTimer -- Function to select a mole local function selectNewMole( subtractOne ) if (lastTimer) then timer.cancel( lastTimer ) end -- Player never tapped mole, take away a point if( subtractOne ) then score = score - 1 scoreLabel.text = "Score: " .. score end -- Clear all moles for i = 1, #moles do moles[i]:setFillColor( 1, 0, 0 ) moles[i].isBlue = false end -- Choose a random mole and make it blue local moleNum = math.random( 1, #moles ) moles[moleNum]:setFillColor( 0, 0, 1 ) moles[moleNum].isBlue = true -- Select a new mole in 1.5 seconds lastTimer = timer.performWithDelay( 1500, function() selectNewMole(true) end ) end -- Common touch listener for moles local touch = function( self, event ) if( event.phase == "began" ) then if( self.isBlue ) then -- Right mole, get a point self.isBlue = false score = score + 1 selectNewMole() else -- Wrong mole, lose a point score = score - 1 end end scoreLabel.text = "Score: " .. score return true end -- Create the 'moles' local moleSize = 120 local moleSpacing = 160 local startX = display.contentCenterX - moleSpacing local startY = display.contentCenterY - moleSpacing for i = 1, 3 do for j = 1, 3 do local mole = display.newRect( startX + (i-1) \* moleSpacing, startY + (j-1) \* moleSpacing, moleSize, moleSize) -- Start all moles as red mole:setFillColor( 1, 0, 0 ) -- Add common touch listener to this mole mole.touch = touch mole:addEventListener( "touch" ) -- Save the mole in our list moles[#moles+1] = mole end end selectNewMole() -- ============================================================= -- END ANSWER -- =============================================================

But this solution is so much more less complicated, less convoluted and less inefficient than my almost working solution.  Thank you.  Now I just have to figure out a couple lines of your code before I am happy.

You’re welcome.  Sorry to code dump like that, but it sounded fun, so I did it.  :unsure:

Paste the troublesome lines here with questions and I’ll see if I can clarify.

The code dump is great.  I will puzzle through the code for a while on my own.  I learn a lot just by the research.  I can tell I have been teaching beginning level programming for too long.  A parent is a CS prof at the local university.  I will  hit him up for answers.  He has been considering using Corona for his summer kid camps and this would be a good one to look at.  If we cannot figure it out I will drop you a note.  Thanks again.