0

我正在尝试构建一种在 Lua 中自动添加和重命名对象的方法。我有用于添加对象的 add 方法,但我不知道如何使它重命名每个对象。我正在考虑添加一个if语句,但我不知道如何构造它,它会重命名它循环的每种类型的对象。

这是我到目前为止所拥有的:

frogBody = {density = .8, friction = 0.3, bounce = 0.1, radius = 10} -- body Type
local onPlayerSpawnObject = function(object) -- method to spawn object
      local layer = map:getTileLayer("Enemies")
      local frog = movieclip.newAnim{ "FrogMini.png", "frogMiniRed.png" } -- object that spawns
      frog.x = object.x ; frog.y = object.y
      frog.myName = "frog"
      frog.isHit = false
      physics.addBody(frog, frogBody)
end

感谢您的所有帮助!

4

1 回答 1

0

我认为你的意思是你想从你的例子中独特地访问青蛙对象?

如果是这种情况,您可以将 add 函数的返回值分配给 lua 表,或者直接分配给函数内的表。

local frogs = {}
frogBody = {density = .8, friction = 0.3, bounce = 0.1, radius = 10} -- body Type
local onPlayerSpawnObject = function(object) -- method to spawn object
    local layer = map:getTileLayer("Enemies")
    local frog = movieclip.newAnim{ "FrogMini.png", "frogMiniRed.png" } -- object that spawns
    frog.x = object.x ; frog.y = object.y
    frog.myName = "frog"
    frog.isHit = false
    physics.addBody(frog, frogBody)
    table.insert(frogs, frog)
end

-- To print a distinct frog from the collection, replace 1 by your index
print (frogs[1].myName)
于 2013-10-21T10:10:20.037 回答