Changing text color according to values.

Hi is there any ways to color the color of a text according to the different range of values??

Eg:

if Marks[1] \<= 50 then Results = "Pass" elseif Marks[1] \<= 60 and Marks[1] \> 50 then Results = "Good Job"&nbsp;

and when printing out

local score = display.newText( Results[1], 0, 0, "Helvetica", 32 ) --score:setFillColor( 0 ) -- black score.x = display.contentWidth - 165 score.y = 200

 What i want is that when I open the app it gives me a random mark and would print out the results.

How should I do it so that it is not hard coded? Like when the system gives me a value of 60 it would display “Good Job” in Blue color while “Pass” is in red color.

Thanks

Well, you have to set it somewhere. I’d be inclined to wrap the whole thing in a function something like :

function createScoreText(marks,x,y) local textObj = display.newText("",0,0,"Helvetica",32) textObj.x = x textObj.y = y if marks \<= 50 then textObj.text = "Pass" textObj:setFillColor(1,0,0) elseif marks \> 50 and marks \<= 60 then textObj.text = "Good Job" textObj:setFillColor(0,0,1) end return textObj end

You may want to extract the pass/fail tests into a separate function if you use this elsewhere.  The idea is that if you change the pass mark (say) or colour scheme you only need to do it in one place.

Well, you have to set it somewhere. I’d be inclined to wrap the whole thing in a function something like :

function createScoreText(marks,x,y) local textObj = display.newText("",0,0,"Helvetica",32) textObj.x = x textObj.y = y if marks \<= 50 then textObj.text = "Pass" textObj:setFillColor(1,0,0) elseif marks \> 50 and marks \<= 60 then textObj.text = "Good Job" textObj:setFillColor(0,0,1) end return textObj end

You may want to extract the pass/fail tests into a separate function if you use this elsewhere.  The idea is that if you change the pass mark (say) or colour scheme you only need to do it in one place.