2017-01-30 53 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,则计时器暂停并且不再继续。 。 –