How do I set multiple colors? Help please.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Ziggy2764
Prole
Posts: 6
Joined: Sat Aug 25, 2012 3:45 pm

How do I set multiple colors? Help please.

Post 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)
Bannana97
Citizen
Posts: 77
Joined: Wed May 16, 2012 2:49 pm

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

Post by Bannana97 »

Simply recall the setColor method.

love.graphics.setColor(...)
love.graphics.rectangle(...)
love.graphics.setColor(...)
love.graphics.rectangle(...)
Ziggy2764
Prole
Posts: 6
Joined: Sat Aug 25, 2012 3:45 pm

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

Post 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
cobrajs
Prole
Posts: 2
Joined: Tue Apr 24, 2012 1:25 am

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

Post 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
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

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

Post 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.
Ziggy2764
Prole
Posts: 6
Joined: Sat Aug 25, 2012 3:45 pm

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

Post by Ziggy2764 »

Ok, I got it. Thanks guys.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 6 guests