【发布时间】:2021-12-21 20:56:52
【问题描述】:
下面是我的一个不言自明的love2d程序示例。变量“状态”决定了游戏的状态,即“游戏”和“菜单”。初始状态是“菜单”,这里预期发生的情况是,当第一次右键单击时,状态变为“播放”,然后在第二次右键单击时,变量 printMsg 设置为 true,结果显示一条消息打印在函数love.draw()内。
function love.load()
state = 'menu'
end
function love.draw()
if printMsg == true then
love.graphics.print('mousepressed')
end
end
function love.mousepressed(x, y, button)
if state == 'menu' and button == 1 then
state = 'play'
end
if state == 'play' and button == 1 then
printMsg = true
end
end
我有两个问题:
-
在第一次单击时会打印消息,因为程序倾向于认为第一次单击也是第二次单击。
-
无需创建变量
printMsg来实际打印消息,我想在按下按钮的实例上打印消息。我的意思是:
function love.load()
state = 'menu'
end
function love.draw()
end
function love.mousepressed(x, y, button)
if state == 'menu' and button == 1 then
state = 'play'
end
if state == 'play' and button == 1 then
love.graphics.print('mousepressed')
end
end
但不幸的是,这不会打印任何内容。
感谢任何答案。
【问题讨论】:
-
文本应该显示多长时间?如果我第三次点击会发生什么?
-
只要 state='play',文本就会显示。第三次点击没有动作。
-
谢谢,但这是一个完全不同的话题
-
那么在你的原始代码中使用
elseif就可以了。