So, here it is. It's pretty much the same as the current callbacks tutorial, but since it's already written it'd just dumb to keep it sitting here on my HD n_n
Using input
Capturing input events with LÖVE is really easy; in this tutorial we'll cover how to capture keyboard and mouse events using both object methods and callbacks.
We'll start our tutorial by putting together an almost empty LÖVE program:
Code: Select all
function load()
love.graphics:setFont(love.objects:newFont(love.default_font, 12))
text = “”
end
function update(dt)
end
function draw()
love.graphics:draw( “text, 330, 300 )
end
Capturing keyboard events:
The most easy way to know if the user is pressing a key is calling the isDown method which has the following syntax:
The key parameter is the constant value of the key we want to see if it's currently pressed. A simple example:
Code: Select all
if love.keyboard:isDown( love.key_space ) then
text = “SPACE is being pressed!”
end
You can find the complete list of key constants
here
The best place to perform this check is inside the
update() callback: that way we're able to get input from the user and update our variables
before drawing our stuff into the screen. So, our modified
update() callback should look like this:
Code: Select all
function update(dt)
if love.keyboard:isDown( love.key_space ) then
text = “SPACE is being pressed!”
end
end
While this is fine and dandy if we only need to know which key or keys are currently pressed, we might also need to specify different behaviors when a certain key is pressed and/or released. An elegant way of doing this is using the keyboard callbacks
keypressed() and
keyreleased(). They work in a similar way of the already known
update() or
draw() callbacks, executing our code every time that event is triggered. For example:
Code: Select all
function keypressed( key )
if key == love.key_return then
text = “RETURN is being pressed!”
end
end
function keyreleased( key )
if key == love.key_return then
text = “RETURN has been released!”
end
end
As you can see, these two callbacks will provide you a
key variable which you can use to check if a given key has been pressed, released or both. Up to this point, our source file should look like this:
Code: Select all
function load()
love.graphics:setFont(love.objects:newFont(love.default_font, 12))
text = ""
end
function update(dt)
if love.keyboard:isDown( love.key_space ) then
text = “SPACE is being pressed!”
end
end
function draw()
love.graphics:draw( text, 330, 300 )
end
function keypressed( key )
if key == love.key_return then
text = “RETURN is being pressed!”
end
end
function keyreleased( key )
if key == love.key_return then
text = “RETURN has been released!”
end
end
Capturing mouse events:
So, we already know how to interact with our users through a keyboard. But what about that little rodent that sits on their desks? Well, mouse input works in a fairly similar way: we have an
isDown() method and the
mousepressed() and
mousereleased() callbacks.
Let's add a few lines to our
update() callback:
Code: Select all
if love.mouse:isDown(love.mouse_right) then
text = “Mouse button right is pressed”
end
As you can see, it's very similar to the keyboard's
isDown() method and, again, you can see the full list of mouse-related constants
here. You can even check if the mouse wheel has been rolled up or down using this method.
We also have two handy methods to know the current position of the mouse pointer inside our game window:
getX() and
getY(). Each one will return the current coordinate of the mouse pointer. Let's see an example by adding these line to the beginning of our
update() callback:
Code: Select all
mouse_x = love.mouse:getX()
mouse_y = love.mouse:getY()
And this line to our draw() callback:
Code: Select all
love.graphics:draw( “Mouse X: ”.. mouse_x .. “ Mouse Y: ” .. mouse_y, 10, 20 )
The
mousepressed() and
mousereleased() callbacks work in a very similar way as their keyboard counterparts:
Code: Select all
function mousepressed(x, y, button)
if button == love.mouse_left then
text = “Mouse button left is pressed”
end
end
function mousereleased(x, y, button)
if button == love.mouse_left then
text = “Mouse button left is pressed”
end
end
A cool feature of this callback is that you can know not only if a button has been pressed but also the position of the mouse pointer when the user pressed the button. This can be really useful if you need to put together some basic user interface elements like buttons or other objects that can interact with the mouse. A simple example:
Code: Select all
function mousepressed(x, y, button)
if button == love.mouse_left then
text = “Mouse button left is pressed at X:”..x.." Y: "..y
end
end
Finally, we have yet another two useful mouse-related methods:
setVisible() and
isVisible(). The first one will let you hide or show the mouse pointer and the second one will obviously let you know if the mouse pointer is whether visible or not. Let's add even more code to our
update() callback:
Code: Select all
if love.keyboard:isDown(love.key_h) then
if love.mouse:isVisible then
love.mouse:setVisible(false)
else
love.mouse:setVisible(true)
end
end
In these few lines we check if the mouse pointer is visible or not and then we change its visibility: if it's visible we hide it and if it's already hidden we then show it. Fairly easy, isn't it?
-- END OF TUTORIAL
Teh screenshot:
Teh files:
http://www.cloverpunk.com.ar/tehstuffs/love-tut1.zip