Power Button For Putt Putt Game

I am trying to make a power button that when you tap it once, it starts a timer, and then when you tap it twice, it stops the timer and then correlates that time period with a power value. 

I started off by creating a button that changes the location of text “JK” to make sure the tapping was working. I was also trying to print out the time difference between the first tap and the second tap, but the printout does not appear.

local textObject = display.newText(“JK”, 50, 50, nil, 24)

textObject: setTextColor(255, 255, 255)

local myButton = 

        display.newImage(“power-button-red-md2.png”) 

myButton:scale(0.5, 0.5)

function myButton:tap (event)

textObject.x = math.random(0, display.stageWidth)

textObject.y = math.random(0, display.stageHeight)

end

myButton:addEventListener(“tap”, myButton)

myButton.markTime = system.getTimer()

– enterFrame listener function

local function trackTime(tap)

    local secondsPassed = (tap.time - myButton.markTime) / 1000

    if secondsPassed >= timeThresh then

        display.newText( “You held down the button for at least " … timeThresh … " seconds.” )

                Runtime:removeEventListener( “enterFrame”, trackTime )

    end

end

I would appreciate some help.

Hi @johnjkimster,

I imagine the tap function should look closer to this:

 

[lua]

local secondsPassed

myButton.gettingTime = false

 

function myButton:tap (event)

   if ( myButton.gettingTime == false ) then

      myButton.gettingTime = true

      myButton.markTime = system.getTimer()

   elseif ( myButton.gettingTime == true ) then

      myButton.gettingTime = false

      secondsPassed = system.getTimer() - myButton.markTime

   end

end

 

myButton:addEventListener(“tap”, myButton)

[/lua]

 

Regards,

Brent

Hi @johnjkimster,

I imagine the tap function should look closer to this:

 

[lua]

local secondsPassed

myButton.gettingTime = false

 

function myButton:tap (event)

   if ( myButton.gettingTime == false ) then

      myButton.gettingTime = true

      myButton.markTime = system.getTimer()

   elseif ( myButton.gettingTime == true ) then

      myButton.gettingTime = false

      secondsPassed = system.getTimer() - myButton.markTime

   end

end

 

myButton:addEventListener(“tap”, myButton)

[/lua]

 

Regards,

Brent