我正在尝试在我的 CS50G 五十只鸟问题集中创建一个暂停功能。然而,渲染时我必须呈现的图像位于管道和鸟的后面。我是 Love2d 和 Lua 的新手,但我没有找到任何有关如何在按下按键时将图像移动到前面的文档。
function PlayState:update(dt)
if love.keyboard.wasPressed('p') then
if self.pause then
self.pause = false
scrolling = true
sounds['music']:play()
sounds['pause']:pause()
else
self.pause = true
scrolling = false
sounds['music']:pause()
sounds['pause']:play()
end
end
if not self.pause then
function PlayState:render()
for k, pair in pairs(self.pipePairs) do
pair:render()
end
if self.pause then
love.graphics.draw(pause, 235, 100, center)
end
love.graphics.setFont(flappyFont)
love.graphics.print('Score: ' .. tostring(self.score), 8, 8)
self.bird:render()
我对代码进行了一些修改,并尝试将图片移动到 main.lua,但这也不起作用,或者我只是做错了。
将我的评论转换为答案...
绘图从下到上按层进行,因此将暂停绘图移动到您正在绘制的其他实体下方(发生在其之后)应将其放置在顶层:
-- ...
for k, pair in pairs(self.pipePairs) do
pair:render()
end
love.graphics.setFont(flappyFont)
love.graphics.print('Score: ' .. tostring(self.score), 8, 8)
self.bird:render()
if self.pause then -- moved down to bottom
love.graphics.draw(pause, 235, 100, center)
end
-- ...