0

我正在尝试创建一个名为 star1vis( - 的代码star1vis = 0) 的变量,当发生碰撞时,star1vis 为 1 ( 代码 - ..... then star1vis = 1)。在另一个名为 level1com 的 Lua 文件中,我使用的是全局变量,因此如果发生碰撞,如果没有发生碰撞,那么我可以让星星可见,那么该星星是不可见的。我对 GML(Game Maker Language - 类似于 C++)有丰富的经验,在那种语言中我就是这样做的,所以我的问题是如何在 lua 语言中创建该变量,因为我得到 nil 值错误 - 没有那个代码它是工作完美。

4

1 回答 1

1

It's the most important thing that, where did you first created star1vis variable?

If you created it in a module, lets say module1.lua, then you can reach it easly with module1.star1vis

But if you created star1vis in main.lua, you may overwrite it in another module. See exapmle below:

main.lua:

.....
star1vis = 0 --star1vis first created here
.....

module1.lua:

.....
star1vis = 1 --you overwrite star1vis here. That means there are 2 star1vis variables with same names in different modules right now. 
-----
-----
print( star1vis ) --if you do this without creating a new variable in module1.lua, this will reach the variable in main.lua.
-----

I guess you want to have your variable in main.lua and be able to change it from another modules. If so, here is what you have to do:

main.lua:

----
star1vis = 0  --first created
function changestar1vis( new )
    star1vis = new
end
-----

module1.lua:

------
changestar1vis( 0 )
------

Hope it helps...

于 2013-03-31T12:07:34.650 回答