2017-04-26 31 views
0

我想在幾秒鐘後更改文本對象的字符串。這是代碼:用定時器更改文本

if scoretoShow then 
aAscoretext = display.newText(scoretoShow.."/8", 200, 200, "Comic Sans MS", 30) 
else 
aAscoretext = display.newText("0/8", 200, 200, "Comic Sans MS", 30) 
end 
aAscoretext:setFillColor(0.4) 
aAscoretext.x = Letterbackground.x 
aAscoretext.y = Letterbackground.y + Letterbackground.width/3 


    if type(scoreChange) == "number" then 
for f = 0, scoreChange, 1 do 
    print("f") 
     print(f) 

timer.performWithDelay(timeforChange-(f*(timeforChange/scoreChange)), 
function() 
aAscoretext.text = "HELLO!" 
Letterbackground:setFillColor(gradients[newScore-f+1]) 
audio.play(Wobble) 
end) 

但是,如果我把aAscoretext.text = "HELLO!"剛下print(f)然後正常工作。

謝謝。

+0

什麼是你的問題? – ldurniat

+0

我希望文本aAscoretext改爲閱讀HELLO!經過定時器設置的延遲後。它不這樣做。我想知道爲什麼不。 – Atrag

+0

在代碼片段中缺少兩個'end'語句(一個關閉'for'塊,另一個關閉'if type(scoreChange)==「number」then')。 – GoojajiGreg

回答

0

for循環中對timer.performWithDay()的所有調用可能未按照您期望的方式執行。如果需要更改另一個步驟,讓更改TextObject中text屬性的偵聽器在延遲後再次調用自己,我覺得更容易。

這將說明我的意思。假設您有一個TextObject score,它顯示得分和兩個變量currentScorenewScore,其中newScore > currentScore和兩者都是整數值。如果你想將比分顯示從currentScore「計數」來newScore,你可以這樣說:

local currentScore = 0 

-- 
-- Call this function when the score needs to change 
-- 
local function newScoreToDisplay(newScore)  

    local countingInterval = 100 -- we want to add 1 to the displayed score every 100 ms 

    local function adjustScoreDisplay(event) 

     if currentScore < newScore then 
      currentScore = currentScore + 1 
      score.text = tostring(currentScore) -- score is a Corona TextObject 
      timer.performWithDelay(countingInterval, adjustScoreDisplay) 
     end 
    end 

    -- First call to adjustScore() if score really needs adjusting 
    if currentScore < newScore then 
     timer.performWithDelay(0, adjustScore) --make the first change immediately 
    end 
end