2017-01-30 74 views
0

當觸摸按鈕時,Corona SDK定時器countUp會加速嗎?我有我的第15場的問題,每次的答案是被觸摸定時器計數進位加速每次它進入另一個問題..當觸摸按鈕時,Corona SDK定時器countUp會加速嗎?

這裏是我的按鈕觸摸事件

buttonTouched = function(event) 
    local t = event.target 
    local id = t.id 

    if event.phase == "began" and touchEnabled == true then 
     display.getCurrentStage():setFocus(t) 
     t.isFocus = true 

     if id == "answer" then 
      t.alpha = 0.6 
     else 
      t.xScale = 0.9 
      t.yScale = 0.9 
     end 

    elseif t.isFocus then 
     if event.phase == "ended" then 
      display.getCurrentStage():setFocus(nil) 
      t.isFocus = false 

      if id == "answer" then 
       t.alpha = 1 
      else 
       t.xScale = 1 
       t.yScale = 1 
      end 

      -- Check that touch finished in the button. 
      local b = t.contentBounds 
      if event.x >= b.xMin and event.x <= b.xMax and event.y >= b.yMin and event.y <= b.yMax then     
       utils.playSound("select") 

       if id == "answer" then 
        if timer_trans ~= nil then 
         transition.cancel(timer_trans) 
         timer_trans = nil 
        end 

        if result_trans ~= nil then 
         transition.cancel(result_trans) 
         result_trans = nil 
        end 

        if label_result ~= nil then 
         display.remove(label_result) 
         label_result = nil 
        end 

        -- Show some text that we can transition 
        label_result = display.newText({parent=uiGroup, text="", font=easyFont, fontSize=75}) 
        label_result.anchorX = 0 
        label_result.x = label_question.x - 540 
        label_result.y = label_question.y + 400 

        if t.index == questions[onQuestion].answer then 
         label_result.text = "Correct!" 
         label_result:setFillColor(0,0.6,0) 
         utils.playSound("score") 
         updateScore(1) 

        else 
         label_result.text = "Incorrect..." 
         label_result:setFillColor(0.8,0,0) 
         utils.playSound("incorrect") 
        end 

        result_trans = transition.to(label_result, {time=1600, alpha=0.1, y=label_result.y-18,tag="transTag", onComplete=function() 
         display.remove(label_result) 
         label_result = nil 
        end}) 

        -- Now create the next quesiton 
        createQuestion() 
       end 
      end 
     end 
    end 
    return true 
end 

function startTimer() 
    clockTimer = timer.performWithDelay(1000,doCountUp,gameTime) 
end 

function doCountUp() 

    currentTime = countUpText.text 
    currentTime = currentTime +1 
    countUpText.text = currentTime 
    if(currentTime == 0) then 
     countUpText.text = currentTime 
     startTimer() 
    end 
end 
+0

我無法在您的代碼計時器中找到'countUp'名稱。 – ldurniat

+0

我currentTime =當前時間+1是爲countUp我已經編輯它我的錯誤大聲笑.. –

回答

1

的計時器「加速「,因爲不是重置當前計時器,而是在單擊問題時創建新計時器。

你叫startTimer所()每次創建一個新的問題 (假設你在createQuestion()設置countUpText.text = "-1"。每當你觸摸的響應時間再創建一個定時器來更新countUpText.text。你有多個計時器更新文本,因爲你不「T刪除先前創建的計時器你是剛剛建立新的

解決這個問題的最簡單方法是取消計時器並開始,如果計時器已經創建了一個新:

local clockTimer 

function startTimer() 
    if (clockTimer ~= nil) then 
     timer.cancel(clockTimer) 
     clockTimer = nil 
    end 

    clockTimer = timer.performWithDelay(1000,doCountUp,gameTime) 
end 

所以更新你的startTimer()功能,然後將local clockTimer添加到您的Lua文件的頂部。

+0

謝謝大衛我已經做了buttonTouched同樣的事情。它不再加速。但事情是,如果通過「觸摸」連續快速地使用buttonTouched,則計時器暫停並且不再繼續。 。 –