接下来的几篇 demo 都很简单,一片一片的写没什么意义,说起来之前三篇其实也可以合起来,事已至此,先这样吧
[5] 获取鼠标按键,代码本身就已经做了很好地说明了:
function love.load()
love.graphics.setFont(love.graphics.newFont(11))
end
function love.draw()
-- Left mouse button.
if love.mouse.isDown(1) then
love.graphics.print("Left mouse button is down", 50, 50)
end
-- Right mouse button.
if love.mouse.isDown(2) then
love.graphics.print("Right mouse button is down", 50, 100)
end
-- Middle mouse button.
if love.mouse.isDown(3) then
love.graphics.print("Middle mouse button is down", 50, 75)
end
end
[6] 设置鼠标可见性,配合之前说的图片跟随鼠标,就可以设置自定义鼠标样式了:
function love.load()
-- Hide mouse on startup.
love.mouse.setVisible(false)
love.graphics.setFont(love.graphics.newFont(11))
end
-- Toggle cursor visibility.
function love.keypressed(k)
if k == "v" then
if love.mouse.isVisible() then
love.mouse.setVisible(false)
else
love.mouse.setVisible(true)
end
end
end
function love.draw()
love.graphics.print("Press V to toggle visibility.", 50, 50)
end
需知道的是,这个 love.keypressed 方法,只需要声明,不需要调用,可以想象这个方法是一直在线的。
[7] 挂起,作用大概就是阻塞 update 方法吧(示例看不出个啥效果)
function love.update(dt)
-- Sleeps 10ms after each udpate. By doing this,
-- CPU time is made available for other processes,
-- and your OS will love you for it.
love.timer.sleep(0.01)
end
[8] 忘了咋翻译了,分别代表了一秒钟的帧数和两帧时间间隔
function love.load()
love.graphics.setFont(love.graphics.newFont(11))
end
function love.draw()
-- Draw the current FPS.
love.graphics.print("FPS: " .. love.timer.getFPS(), 50, 50)
-- Draw the current delta-time. (The same value
-- is passed to update each frame).
love.graphics.print("dt: " .. love.timer.getDelta(), 50, 100)
end
[9] 获取时间毫秒数(至于从何开始计算的就不清楚了,自查 Wiki 吧)
function love.load()
-- Get time before the code to be timed.
t_start = love.timer.getTime()
-- Load 10 fonts.
for i=13,22 do
local f = love.graphics.newFont(i)
love.graphics.setFont(f)
end
-- Get time after.
t_end = love.timer.getTime()
end
function love.draw()
love.graphics.print("Spent ".. (t_end-t_start) .. " seconds loading 10 fonts.", 50, 50)
end
[10] 获取按键
function love.load()
love.graphics.setFont(love.graphics.newFont(11))
end
function love.draw()
-- Checks whether the return key is down or not.
if love.keyboard.isDown("return") then
love.graphics.print("The return key is down.", 50, 50)
else
love.graphics.print("The return key isn't down.", 50, 50)
end
end
下一篇预告:创建使用动画
代码挺多的,需要好好看看 😁
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于