How Mix Love.Draw and Love.Update?

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
Krizzu
Prole
Posts: 20
Joined: Sun Apr 15, 2012 8:02 pm

How Mix Love.Draw and Love.Update?

Post by Krizzu »

Hello

I got problem. I want to make a information about leaving the game.

So, When I press Escape Key (in love.keypressed function) I want to print in the middle of the screen text "Are you sure want to quit"(love.draw)

Code: Select all

function love.keypressed(key)  
   if key == "escape" then
		love.event.quit()
   end
end

anyone know how to make it?
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: How Mix Love.Draw and Love.Update?

Post by Santos »

You can do this by having a variable to hold the state the game is in.

It can be set first in love.load.

Code: Select all

function love.load()
	state = 'play'
end
If the state is 'play' and the escape key is pressed, you can then set the state to something else, e.g. 'quitting'.

And if the state is 'quitting' and some special key is pressed (like y for yes), then the game can quit.

And if the state is 'quitting' and some other key was pressed, then the state can change back to 'play'.

Code: Select all

function love.keypressed(key)
	if state == 'play' then
		if key == 'escape' then
			print('this happens?')
			state = 'quitting'
		end
	elseif state == 'quitting' then
		if key == 'y' then
			love.event.quit()
		else
			state = 'play'
		end
	end
end
What is drawn in love.draw can be changed depending on the state.

Code: Select all

function love.draw()
	love.graphics.print('Testing! The state is '..state, 10, 10)

	if state == 'quitting' then
		love.graphics.print('Are you sure you want to quit? (press "y" to quit)', 250, 250)
	end
end
A .love file is attached with this example.

I hope this helps! ^^
Attachments
confirm.love
(415 Bytes) Downloaded 88 times
User avatar
Krizzu
Prole
Posts: 20
Joined: Sun Apr 15, 2012 8:02 pm

Re: How Mix Love.Draw and Love.Update?

Post by Krizzu »

Helps a lot! Thanks buddy!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests