英文:
Is there a way for me to avoid using a global variable here?
问题
所以我刚开始学习Lua和Love2D,并且在需要回调函数相互通信时使用全局变量。例如:
enter_pressed = false
function love.update(dt)
if love.keyboard.isDown('return') then
enter_pressed = true
end
end
function love.draw()
if enter_pressed then
love.graphics.print("Enter has been pressed", 100, 200)
end
有没有办法避免编写这样的代码?
除了搜索可能的解决方案外,我还没有尝试过任何方法。
英文:
So I just started learning Lua and Love2D and am using global variables when I need callback functions to communicate with eachother. For example:
enter_pressed = false
function love.update(dt)
if love.keyboard.isDown('return') then
enter_pressed = true
end
end
function love.draw()
if enter_pressed then
love.graphics.print("Enter has been pressed", 100, 200)
end
Is there any way to avoid having to write such code?
I haven't tried anything yet except for googling for possible solutions.
答案1
得分: 2
只使用本地变量:
local enter_pressed = false
如果您想限制其范围,请将其放在do...end
块内并声明为本地变量:
do
local enter_pressed = false
function love.update(dt)
...
end
function love.draw()
...
end
end
这是在Lua中定义类似C风格的静态变量的方式。
英文:
Just use a local variable:
local enter_pressed = false
If you want to limit its scope, make it local inside a do...end
block:
do
local enter_pressed = false
function love.update(dt)
...
end
function love.draw()
...
end
end
That's the way to define C-style static variables in Lua.
答案2
得分: 1
以下是已翻译的代码部分:
这取决于上下文,但从技术上讲,update()
和 draw()
方法的工作方式相似,因此,如果您只想快速获得结果,您也可以将语句放在 draw
函数中:
function love.draw()
if love.keyboard.isDown('return') then
love.graphics.print("Enter has been pressed", 100, 200)
end
end
另外,我还在使用全局变量来让 update()
和 draw()
之间进行通信,不过是否使用全局变量来处理按钮按下的情况取决于游戏的上下文。
例如:如果您想使用按钮按下来移动一个对象,那么您可以将这些操作放在对象的 update
函数中,然后让 draw()
函数只关注绘制该对象。
英文:
This is depending on the context, but technically, the update() and draw() methods work similair, so if you just want a quick result, you could also put the statement in the draw function:
function love.draw()
if love.keyboard.isDown('return') then
love.graphics.print("Enter has been pressed", 100, 200)
end
end
Otherwise, I'm also using global variables to let update() and draw() communicate with each other, though whether or not I use gobal variables for button presses depends on the context of the game.
Ex: if you want to use button presses to move an object, then you can put these actions in an object's update function, and then let the draw() only focus on drawing the said object.
答案3
得分: 0
翻译好的部分如下:
"Even love can be local in LÖVE"
只是尝试一下...
-- main.lua
local enter_pressed = false
local love = require('love')
function love.update(dt)
if love.keyboard.isDown('return') then
enter_pressed = true
end
end
function love.draw()
if enter_pressed then
love.graphics.print("Enter has been pressed", 100, 200)
end
end
PS: LÖVE创建的对象通常具有UserData,您可以在其中存储数据,如果您愿意的话。
参见示例:https://love2d.org/wiki/Body:setUserData
PPS: 即使不是初学者,也值得一看...
https://love2d.org/wiki/Config_Files
...并使用它
英文:
Even love
can be local in LÖVE
Just try it...
-- main.lua
local enter_pressed = false
local love = require('love')
function love.update(dt)
if love.keyboard.isDown('return') then
enter_pressed = true
end
end
function love.draw()
if enter_pressed then
love.graphics.print("Enter has been pressed", 100, 200)
end
end
PS: LÖVE created Objects often has UserData
where you can store such Data if you like
See for Example: https://love2d.org/wiki/Body:setUserData
PPS: Also and not even for Beginners it is worth a look on...
https://love2d.org/wiki/Config_Files
...and use it
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论