Page 1 of 1

How do I set multiple colors? Help please.

Posted: Sat Aug 25, 2012 11:28 pm
by Ziggy2764
I was wondering if it is possible to set 2 colors for something. I use love.graphics.setColor(r, g, b) to set a color for something (I'm not even sure if theres another way), but you cant set 2 different colors, it's either one or none. Does anyone know if theres a way to set a color for a specfific object and not everything?

Here's my code, it may help you understand what I'm asking help on. In my code, you will see one lua.graphics.setColor, I want the second one to be under the finish1_draw() function, so all the "walls" will be 158, 158, 158 (gray), and the finishline will be a different color.

My code:

Code: Select all

finishline = love.graphics.newImage("Images/finish.png")

function level1_draw()
	love.graphics.rectangle("fill", 410, 150, 40, 350) --Left Wall
	love.graphics.rectangle("fill", 490, 150, 40, 350) --Right Wall
	love.graphics.rectangle("fill", 410, 150, 120, 40) --Top Wall
	love.graphics.rectangle("fill", 410, 500, 120, 40) --Bottom Wall
	love.graphics.setColor(158, 158, 158)
	
	if player.x < 451 or
	player.x > 465 or
	player.y < 191 or
	player.y > 474 then
		player.x = startPosX
		player.y = startPosY
	
	end
	
	function finish1_draw()
		love.graphics.draw(finishline, 458, 200)
	
	end

end
(I have more code for the game that I'm working on, but I dont think it really matters, because I'm just focusing on the colors)

Re: How do I set multiple colors? Help please.

Posted: Sat Aug 25, 2012 11:30 pm
by Bannana97
Simply recall the setColor method.

love.graphics.setColor(...)
love.graphics.rectangle(...)
love.graphics.setColor(...)
love.graphics.rectangle(...)

Re: How do I set multiple colors? Help please.

Posted: Sun Aug 26, 2012 12:02 am
by Ziggy2764
@bannana I tried putting the setColor in both places where I wanted it, but everything turned to one of the colors instead of each of them having their own color.

This is how I had it with both, but everything turned to the color of the finishline, in this case, white:

Code: Select all

finishline = love.graphics.newImage("Images/finish.png")

function level1_draw()
   love.graphics.rectangle("fill", 410, 150, 40, 350) --Left Wall
   love.graphics.rectangle("fill", 490, 150, 40, 350) --Right Wall
   love.graphics.rectangle("fill", 410, 150, 120, 40) --Top Wall
   love.graphics.rectangle("fill", 410, 500, 120, 40) --Bottom Wall
   love.graphics.setColor(158, 158, 158)
   
   if player.x < 451 or
   player.x > 465 or
   player.y < 191 or
   player.y > 474 then
      player.x = startPosX
      player.y = startPosY
   
   end
   
   function finish1_draw()
      love.graphics.draw(finishline, 458, 200)
      love.graphics.setColor(255, 255, 255)
   
   end

end

Re: How do I set multiple colors? Help please.

Posted: Sun Aug 26, 2012 12:27 am
by cobrajs
You need to have the setColor immediately before the drawing of the rectangle. It draws using whatever the last color was, and white is set after the other color is set, so white overwrites it.

You'll want something like this:

Code: Select all

       love.graphics.setColor(158, 158, 158)
       love.graphics.rectangle("fill", 410, 150, 40, 350) --Left Wall
       love.graphics.setColor(158, 158, 158)
       love.graphics.rectangle("fill", 490, 150, 40, 350) --Right Wall
       love.graphics.setColor(158, 158, 158)
       love.graphics.rectangle("fill", 410, 150, 120, 40) --Top Wall
       love.graphics.setColor(158, 158, 158)
       love.graphics.rectangle("fill", 410, 500, 120, 40) --Bottom Wall

Re: How do I set multiple colors? Help please.

Posted: Sun Aug 26, 2012 11:57 am
by T-Bone
Also, pro tip, always reset the color with love.graphics.setColor(255,255,255) when you're done, or in the beginning of love.draw. That way you only color the things you want to color, instead of everything.

Re: How do I set multiple colors? Help please.

Posted: Sun Aug 26, 2012 4:33 pm
by Ziggy2764
Ok, I got it. Thanks guys.

Re: How do I set multiple colors? Help please.

Posted: Fri Aug 31, 2012 6:38 am
by Robin
cobrajs wrote:You'll want something like this:
No. Those are all the same colour, so this works just as well:

Code: Select all

       love.graphics.setColor(158, 158, 158)
       love.graphics.rectangle("fill", 410, 150, 40, 350) --Left Wall
       love.graphics.rectangle("fill", 490, 150, 40, 350) --Right Wall
       love.graphics.rectangle("fill", 410, 150, 120, 40) --Top Wall
       love.graphics.rectangle("fill", 410, 500, 120, 40) --Bottom Wall
Don't go overboard with using setColor before each drawing operation. That only makes your code harder to read.