Reputation: 743
I want to change the string of a text object after a number of seconds. This is the code:
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)
However, if I put the aAscoretext.text = "HELLO!"
just under print(f)
then it works fine.
Thank you.
Upvotes: 0
Views: 206
Reputation: 1183
All those calls to timer.performWithDay()
in the for
loop may not be executing the way you expect. I find it easier to make the listener that changes the text
property in the TextObject just call itself again after a delay if another step in the change is needed.
This will illustrate what I mean. Let's say you have a TextObject score
that displays the score and two variables currentScore
and newScore
where newScore > currentScore
and both are integer values. If you want the score display to "count up" from currentScore
to newScore
, you can do it this way:
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
Upvotes: 0