HelloWorld - Facing problem with text color

local textObject = display.newText( "Hello World!", 100, 0, nil, 24 ) textObject:setTextColor( 0,255,255 ) local button = display.newImage( "buttonBlue.png" ) button.x = display.stageWidth / 2 button.y = display.stageHeight - 50 function button:tap( event ) local r = math.random( 0, 255 ) local g = math.random( 0, 255 ) local b = math.random( 0, 255 ) print(r.." "..g.." "..b) transition.to( textObject, { time=1000, y=textObject.y+100 } ) textObject:setTextColor( r, g, b ) end button:addEventListener( "tap", button )

Button is working, animation is working But the text color is not changing.

I just read a pdf tutorial to text the helloworld.​ Why is it not working?

Hi @agrizphpsoft,

Most likely, the tutorial you read is extremely old and outdated. For almost 2 years now, the Corona graphics engine uses 0-1 values for setting colors, not 0-255. So, you should be converting those 255-based values to 0-1 values, either manually or by simple calculation, for example:

[lua]

local r = math.random( 0, 255 ) ; r = r/255  – divide the random value by 255

[/lua]

Hope this helps,

Brent

Or alternatively:

local r = math.random() local g = math.random() local b= math.random()

By default, math.random() returns a value between 0 and 1.

Rob

Hey,

two points:

a) as Brent said, corona uses the color values between 0 and 1… so 0,0,0 is black, 1,1,1 is white, 0.5,0.5,0.5 is grey… and so on

b) don’t use “setTextColor”, but use “setFillColor” instead.  it works on the text object without any problems, I’ve been using it since I started with corona last year.

so just try this:

local textObject = display.newText( "Hello World!", 100, 0, nil, 24 ) textObject:setTextColor( 0,255,255 ) local button = display.newImage( "buttonBlue.png" ) button.x = display.stageWidth / 2 button.y = display.stageHeight - 50 function button:tap( event ) local r = math.random( 0, 255 ) / 255 local g = math.random( 0, 255 ) / 255 local b = math.random( 0, 255 ) / 255 print(r.." "..g.." "..b) transition.to( textObject, { time=1000, y=textObject.y+100 } ) textObject:setFillColor( r, g, b ) end button:addEventListener( "tap", button )

Hi @agrizphpsoft,

Most likely, the tutorial you read is extremely old and outdated. For almost 2 years now, the Corona graphics engine uses 0-1 values for setting colors, not 0-255. So, you should be converting those 255-based values to 0-1 values, either manually or by simple calculation, for example:

[lua]

local r = math.random( 0, 255 ) ; r = r/255  – divide the random value by 255

[/lua]

Hope this helps,

Brent

Or alternatively:

local r = math.random() local g = math.random() local b= math.random()

By default, math.random() returns a value between 0 and 1.

Rob

Hey,

two points:

a) as Brent said, corona uses the color values between 0 and 1… so 0,0,0 is black, 1,1,1 is white, 0.5,0.5,0.5 is grey… and so on

b) don’t use “setTextColor”, but use “setFillColor” instead.  it works on the text object without any problems, I’ve been using it since I started with corona last year.

so just try this:

local textObject = display.newText( "Hello World!", 100, 0, nil, 24 ) textObject:setTextColor( 0,255,255 ) local button = display.newImage( "buttonBlue.png" ) button.x = display.stageWidth / 2 button.y = display.stageHeight - 50 function button:tap( event ) local r = math.random( 0, 255 ) / 255 local g = math.random( 0, 255 ) / 255 local b = math.random( 0, 255 ) / 255 print(r.." "..g.." "..b) transition.to( textObject, { time=1000, y=textObject.y+100 } ) textObject:setFillColor( r, g, b ) end button:addEventListener( "tap", button )