Text color error

I have a function that will change the color of a text once it reaches 5 seconds.
[lua] local function checkTime (event)
if active == true then
if timeRemain <6 and timeRemain~=0 then
–media.playEventSound(buz)
countdowntxt:setTextColor(255,0,0)
growTmr = timer.performWithDelay(.00001, grow, 1)
growTmr = timer.performWithDelay(200, shrink, 1)
end
end
end
codeRed = timer.performWithDelay(1000.0000001, checkTime, 0)[/lua]

But every once in a while i will get this error in the terminal:

Runtime error
…/Desktop/Corona Projects/Warp Speed/easyGame.lua:251: attempt to call method ‘setTextColor’ (a nil value)
stack traceback:
[C]: in function ‘setTextColor’
…mith/Desktop/Corona Projects/Warp Speed/easyGame.lua:251: in function ‘_listener’
?: in function <?:446>
?: in function <?:215> [import]uid: 66117 topic_id: 17361 reply_id: 317361[/import]

need to see more code [import]uid: 7911 topic_id: 17361 reply_id: 65721[/import]

FYI with the tags, you don’t go < lua > then < text > - you put your lua code like this;

< lua > here is my code < / lua >

And that’s it :wink:

As Jstrahan said we really need to see more code than that.

Peach [import]uid: 52491 topic_id: 17361 reply_id: 65736[/import]

Hmm… I’m not sure what code to give you. Let me check… [import]uid: 66117 topic_id: 17361 reply_id: 65745[/import]

@Joe,
what is countdowntxt? You are trying to set the text colour for that, is it a display object that is definitely not nil?

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17361 reply_id: 65751[/import]

What is supposed to be happening is there is a timer that is counting down from thirty seconds. And once it reaches 5 it changes from yellow to red, starts flashing, and this robot guy starts counting “5… 4… 3… 2… 1…” Here is all the necessary code. (I think)
[lua]local countdowntxt = display.newText(timeRemain, _W/2, 130, “Helvetica”, 30)

local function grow (e)
transition.to(countdowntxt, {time=198, xScale = 2, yScale = 2})
end

local function shrink(e)
transition.to(countdowntxt, {time=198, xScale = 1, yScale = 1})
end

local function checkTime (event)
if active == true then
if timeRemain <6 and timeRemain~=0 then
–media.playEventSound(buz)
countdowntxt:setTextColor(255,0,0)
growTmr = timer.performWithDelay(.00001, grow, 1)
growTmr = timer.performWithDelay(200, shrink, 1)
end
end
end
codeRed = timer.performWithDelay(1000.0000001, checkTime, 0)[/lua]
Everything works fine, even with the error… I’m just thinking on the device it might mess things up… [import]uid: 66117 topic_id: 17361 reply_id: 65786[/import]

Out of interest what version of Corona are you using? (Just wondering if it’s a bug with a daily build causing the error or not. Code appears OK.)

Although, OOI, why are you using 1000.0000001?

Peach :slight_smile: [import]uid: 52491 topic_id: 17361 reply_id: 65875[/import]

I’m using Version 2011.591 (2011.8.2).

And the reason I have ten-millionths of a second is because each second I have the number changing, and right after that I want to check to see if it is <6 and >0 so it can do the grow/shrink functions.

Much appreciated,
J.K. [import]uid: 66117 topic_id: 17361 reply_id: 65945[/import]

Try this;

[lua]_W = display.contentWidth
_H = display.contentHeight

timeRemain = 10
active = true

local countdowntxt = display.newText(timeRemain, _W/2, 130, “Helvetica”, 30)

local function countDown(e)
timeRemain = timeRemain - 1
countdowntxt.text = timeRemain
end
timer.performWithDelay(1000,countDown,10)

local function grow (e)
transition.to(countdowntxt, {time=198, xScale = 2, yScale = 2})
end

local function shrink(e)
transition.to(countdowntxt, {time=198, xScale = 1, yScale = 1})
end

local function checkTime (event)
if active == true then
if timeRemain <6 and timeRemain~=0 then
–media.playEventSound(buz)
countdowntxt:setTextColor(255,0,0)
growTmr = timer.performWithDelay(.00001, grow, 1)
growTmr = timer.performWithDelay(200, shrink, 1)
end
end
end
codeRed = timer.performWithDelay(1000, checkTime, 0)[/lua]

Try it in a project on its own. Shouldn’t throw errors.

Peach :slight_smile: [import]uid: 52491 topic_id: 17361 reply_id: 66173[/import]