【问题标题】:Corona SDK timer countUp speeds up when button is touch?触摸按钮时 Corona SDK 计时器计数加快?
【发布时间】:2017-06-15 18:17:15
【问题描述】:

触摸按钮时 Corona SDK 计时器计数加快?我的游戏中确实有 15 个问题,每次触摸答案时,计时器 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

【问题讨论】:

  • 我在您的代码计时器中找不到countUp 名称。
  • 我的 currentTime = currentTime +1 是 countUp 我已经编辑过我的错误大声笑..

标签: timer lua touch coronasdk


【解决方案1】:

计时器正在“加速”,因为当您点击问题时,您正在创建一个新计时器,而不是重置当前计时器。

每次创建新问题时都会调用 startTimer() (假设您在createQuestion() 中设置了countUpText.text = "-1"。每次触摸答案时,您都会创建另一个计时器来更新countUpText.text。您有多个计时器来更新文本,因为您不会删除您刚刚创建的先前创建的计时器新的。

解决此问题的最简单方法是取消计时器并在已创建计时器的情况下启动新计时器:

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 文件的顶部。

【讨论】:

  • 谢谢大卫,我已经用 buttonTouched 做了同样的事情。它不再加速。但问题是,如果您通过“触摸”快速连续使用 buttonTouched,计时器会自行暂停并且不再继续。 .
猜你喜欢
  • 1970-01-01
  • 2013-04-01
  • 2012-02-26
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
相关资源
最近更新 更多