Game Development Reference
In-Depth Information
We can add another function, love.mousepressed , that is invoked every time we use the mouse. We
can position the image at the location where we click the mouse. We reposition the image only if
the left mouse button is pressed.
function love.mousepressed(x1, y1, button)
if button == "l" then
x = x1
y = y1
end
end
You might notice that this is not very smooth, and wouldn't be acceptable in a game setting. You
need to tap the key and then release it every time to make the object move. We could, however,
create some smoother movement based on the cursor keys. To do so, we can move the key-
handling routine out of its own function and into the update function. That way we shall check for
keypresses many more times.
local _W, _H = love.graphics.getWidth(), love.graphics.getHeight()
local dirX, dirY = 10, 10
local theImage, x, y
function love.load()
theImage = love.graphics.newImage("myImage3.png")
x = 50
y = 50
love.graphics.setColor(0,0,0,255)
love.graphics.setBackgroundColor(255,255,255,255)
end
function love.draw()
love.graphics.draw(theImage, x, y)
end
function love.update(dt)
local key = love.keyboard.isDown
if key("up") then
y = y - dirY
elseif key("down") then
y = y + dirY
elseif key("left") then
x = x - dirX
elseif key("right") then
x = x + dirX
end
end
The movement should be much smoother now.
Search WWH ::




Custom Search