如何在Lua中自动添加和重命名对象?

时间:2013-10-21 02:37:10

标签: lua corona

我正在尝试构建一个自动添加和重命名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

感谢您的帮助!

1 个答案:

答案 0 :(得分: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)