I'm new to Love2d. Help me out with text color 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
User avatar
schmavies
Prole
Posts: 5
Joined: Fri Apr 11, 2014 10:55 pm
Location: The Gamma Planetary System, the 58th Planet.

I'm new to Love2d. Help me out with text color please!

Post by schmavies »

I have a problem here. When I run this code:

Code: Select all

require "menu"
function love.load()
	buttonFont = love.graphics.newFont(35)
	love.graphics.setBackgroundColor(255,255,255)
	song = love.audio.newSource("The Bravery.mp3")
	song2 = love.audio.newSource("The Bravery (2).mp3")
	love.audio.play(song)
	song:setLooping(true)
	song2:setLooping(true)
	love.graphics.setColor(0,255,255)
	font = love.graphics.newFont(45)
	smallFont = love.graphics.newFont(15)
	label = "Playing..."
	label2 = "100"
	button_spawn(200,400,"Quit","quit")
end


function love.update()
----- Getting mouse positions for the button
mousex = love.mouse.getX()
mousey = love.mouse.getY()
button_check()



-------------- volume controls
if love.keyboard.isDown("p") then
	love.audio.pause()
	label = "Paused"
end
if love.keyboard.isDown("r") then
	love.audio.play(song)
	label = "Playing..."
end
if love.keyboard.isDown("escape") then
	love.event.push("quit")
end
if love.keyboard.isDown("1") then
	love.audio.setVolume(.1)
	label2 = "10"
end
if love.keyboard.isDown("2") then
	love.audio.setVolume(.2)
	label2 = "20"
end
if love.keyboard.isDown("3") then
	love.audio.setVolume(.3)
	label2 = "30"
end
if love.keyboard.isDown("4") then
	love.audio.setVolume(.4) 
	label2 = "40"
end
if love.keyboard.isDown("5") then
	love.audio.setVolume(.5)
	label2 = "50"
end
if love.keyboard.isDown("6") then
	love.audio.setVolume(.6)
	label2 = "60"
end
if love.keyboard.isDown("7") then
	love.audio.setVolume(.7)
	label2 = "70"
end
if love.keyboard.isDown("8") then
	love.audio.setVolume(.8)
	label2 = "80"
end
if love.keyboard.isDown("9") then
	love.audio.setVolume(.9) 
	label2 = "90"
end
if love.keyboard.isDown("0") then
	love.audio.setVolume(1.0)
	label2 = "100"
end
if love.keyboard.isDown("`") then
	love.audio.setVolume(0)
	label2 = "0"
end
------Switching songs from English to Japanese versions

if love.keyboard.isDown("-") then
	love.audio.stop()
	love.audio.rewind(song2,song)
	song2:play()
end
if love.keyboard.isDown("=") then
	love.audio.stop()
	love.audio.rewind(song,song2)
	song:play()
end



end
function love.draw()
	button_draw()
	love.graphics.setFont(smallFont)
	love.graphics.print(label.." Volume: "..label2,150,100)
	love.graphics.rectangle("fill",150,140,525,20)
	love.graphics.setFont(font)
	love.graphics.print("The Bravery - supercell",150,160)
	love.graphics.setFont(smallFont)
	love.graphics.print("Press 'p' to pause\nPress 'r' to resume\nPress 1-10 to change volume\nPress 'escape' to exit\nPress '-' to switch to Japanese Version\nPress '=' to switch to English Version",150,220)
end

function love_mousepressed(x,y)
	button_click(x,y)
end

and this:

Code: Select all

button = {}

function button_spawn(x,y,text,id)
	table.insert(button, {x = x, y = y, text = text, id = id, mouseover = false})
end

function button_draw()
for i,v in ipairs(button) do
	if v.mouseover == false then
		love.graphics.setColor(0,0,0)
	end
	if v.mouseover == true then
		love.graphics.setColor(0,255,255)
	end
	love.graphics.setFont(buttonFont)
	love.graphics.print(v.text,v.x,v.y)

end
end

function button_click(x,y)
	for i,v in ipairs(button) do
		if x > v.x and 
			x< v.x + buttonFont:getWidth(v.text) and
			y > v.y and 
			y < v.y + buttonFont:getHeight() then
			if v.id == "quit" then
				love.event.push("quit")
			end
			if v.id == "start" then
				gamestate = "playing"
			end
		end
	end
end


function button_check()
for i,v in ipairs(button) do
	if mousex > v.x and 
		mousex < v.x + buttonFont:getWidth(v.text) and
		mousey > v.y and
		mousey < v.y + buttonFont:getHeight(v.text) then
		v.mouseover = true
	else
		v.mouseover = false
	end
end
end
...the program runs okay, but all the text turns black until I select the label and the quit button doesn't work.
What I want it to do is for the color to be (0,255,255) on default for all the text except the button's text (it's default is black), and when I hover over the button, the button will turn to (0,255,255) and when I click on it, it would exit the program...

I would appreciate it if you helped me out! :(
"640K ought to be enough for anybody."
- Bill Gates
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: I'm new to Love2d. Help me out with text color please!

Post by HugoBDesigner »

Okay, I'm guessing this line is where the rectangle is being drawn:

Code: Select all

function love.draw()
   button_draw()
   love.graphics.setFont(smallFont)
   love.graphics.print(label.." Volume: "..label2,150,100)
   love.graphics.rectangle("fill",150,140,525,20) --Is this your button?
   --REST OF THE CODE
end
Just call the love.graphics.setColor function again. The last time your game did it was when it was setting the color for the text. Add this:

Code: Select all

function love.draw()
   button_draw()
   love.graphics.setFont(smallFont)
   love.graphics.print(label.." Volume: "..label2,150,100)
   love.graphics.setColor(0,255,255) --Add this line
   love.graphics.rectangle("fill",150,140,525,20)
   --REST OF THE CODE
end
As for the quit button, I'd probably use "love.event.quit()" instead of "love.event.push("quit")". This should work.

Good luck with your project!
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
schmavies
Prole
Posts: 5
Joined: Fri Apr 11, 2014 10:55 pm
Location: The Gamma Planetary System, the 58th Planet.

Re: I'm new to Love2d. Help me out with text color please!

Post by schmavies »

:crazy: Thanks man! It works like a charm!
"640K ought to be enough for anybody."
- Bill Gates
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests