Love2d Important callbacks
DRAW
function love.load() hamster = love.graphics.newImage(“hamster.png”) x = 50 y = 50 end function love.draw() love.graphics.draw(hamster, x, y) end
FOCUS
function love.load() text = “FOCUSED” end
function love.draw() love.graphics.print(text,0,0) end
function love.focus(f) if not f then text = “UNFOCUSED!!” print(“LOST FOCUS”) else text = “FOCUSED!” print(“GAINED FOCUS”) end end
KEYPRESSED
function love.keypressed(key) if key == “escape” then love.event.quit() end
end
KEYRELEASED
function love.keyreleased(key) if key == “escape” then love.event.quit() end end
-LOAD
function love.load() hamster = love.graphics.newImage(“hamster.png”) x = 50 y = 50 end
function love.draw() love.graphics.draw(hamster, x, y) end
MOUSEFOCUS
function love.load() text = “Mouse is in the window!” end
function love.draw() love.graphics.print(text,0,0) end
function love.mousefocus(f) if not f then text = “Mouse is not in the window!” print(“LOST MOUSE FOCUS”) else text = “Mouse is in the window!” print(“GAINED MOUSE FOCUS”) end end
MOUSEMOVED
love.mousemoved( x, y, dx, dy ) Arguments
number x The mouse position on the x-axis. number y The mouse position on the y-axis. number dx The amount moved along the x-axis since the last time love.mousemoved was called. number dy The amount moved along the y-axis since the last time love.mousemoved was called.
Returns
Nothing.
MOUSEPRESSED
function love.load() printx = 0 printy = 0 end function love.draw() love.graphics.print(“Text”, printx, printy) end function love.mousepressed(x, y, button) if button == “l” then printx = x printy = y end end
MOUSERELEASED
function love.load() printx = 0 printy = 0 end function love.draw() love.graphics.print(“Text”, printx, printy) end function love.mousereleased(x, y, button) if button == “l” then printx = x printy = y end end
QUIT
local quit = true; function love.quit() if quit then print(‘We are not ready to quit yet!’); quit = not quit; else print(‘Thanks for playing. Please play again soon!’); return quit; end return true; end
RESIZE
function love.resize(w, h) print((“Window resized to width: %d and height: %d.”):format(w, h)) end
RUN
The default function for 0.9.0, 0.9.1, and 0.9.2, used if you don’t supply your own. function love.run()
if love.math then
love.math.setRandomSeed(os.time())
for i=1,3 do love.math.random() end
end
if love.event then
love.event.pump()
end
if love.load then love.load(arg) end
-- We don't want the first frame's dt to include time taken by love.load.
if love.timer then love.timer.step() end
local dt = 0
-- Main loop time.
while true do
-- Process events.
if love.event then
love.event.pump()
for e,a,b,c,d in love.event.poll() do
if e == "quit" then
if not love.quit or not love.quit() then
if love.audio then
love.audio.stop()
end
return
end
end
love.handlers[e](a,b,c,d)
end
end
-- Update dt, as we'll be passing it to update
if love.timer then
love.timer.step()
dt = love.timer.getDelta()
end
-- Call update and draw
if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled
if love.window and love.graphics and love.window.isCreated() then
love.graphics.clear()
love.graphics.origin()
if love.draw then love.draw() end
love.graphics.present()
end
if love.timer then love.timer.sleep(0.001) end
end
end
TEXTINPUT
Record and print text the user writes. function love.load() text = “Type away! – “ end
function love.textinput(t) text = text .. t end
function love.draw() love.graphics.printf(text, 0, 0, love.graphics.getWidth()) end
Print text the user writes, and erase text when backspace is pressed.
UPDATE
Run a function called think inside a table called npc once per second. dtotal = 0 – this keeps track of how much time has passed function love.update(dt) dtotal = dtotal + dt – we add the time passed since the last update, probably a very small number like 0.01 if dtotal >= 1 then dtotal = dtotal - 1 – reduce our timer by a second, but don’t discard the change… what if our framerate is 2/3 of a second? npc.think() end end
VISIBLE
function love.visible(v) print(v and “Window is visible!” or “Window is not visible!”);
end
- Previous
- Next