2012-03-17 70 views
3

如何打印()出或找出对象的索引?在Lua中,你如何找出对象存储的关键?

例如,如果我产生了屏幕上的20个随机摇滚对象到一个数组RockTable = {};

喜欢这个RockTable[#RockTable + 1] = rock;

而且所有20个岩石被显示在屏幕上我会如何找出什么键或索引每个岩石通过点击他们?

我正在使用Corona SDK。

任何帮助将不胜感激。

回答

0

不幸的是,你需要蛮力表,据我所知。虽然,要知道点击了一个,你不需要以某种方式循环它们;因此已经知道这个指数了?

编辑

哦,除非Corona有一些点击的回调事件。我从来没有用过它,但我在Lua有经验。

也许你可以做一个向后引用,就像这样:

Rocks = {a rock, a rockB, a rockC} 
RocksB = {[a rock] = 1, [a rockB] = 2, [a rockC] = 3} 

然后只是说rockNum = RocksB [岩]

我敢肯定,应该工作,但我不能保证它,尽管值得一试。

EDIT2

的蛮力方法看起来有点像:

function getRock(rock) 
    for _,v in pairs(rocks) do 
     if (v == rock) 
      return _ 
     end 
    end 
    return "Rock does not exist." 
end 
+1

正如这里所写,Rocks和RocksB是相同的数组。你想要颠倒'RocksB'中的索引和值,也许是'{[a rock] = 1,...'。 – RBerteig 2012-03-21 21:25:46

+0

@RBerteig哦,是啊;我不敢相信我做到了。感谢您指出。 – 2012-03-21 21:28:27

1

你可以做这样的事情,以节省您的在一个表不断循环的一些麻烦找到索引...

RockTable = {} 
RockIndicies = {} 

for i = 1, 20 do 
    idx = #RockTable + 1 
    RockTable[idx] = rock 
    RockIndicies[rock] = idx 
end 

然后当你需要知道索引时,你可以使用你必须为RockIndices建立索引的摇滚来快速获取它。如果你“删除”了一块岩石,你会想要确保在两个地方都将其移除。

4

反转表:

function table_invert(t) 
    local u = { } 
    for k, v in pairs(t) do u[v] = k end 
    return u 
end 

然后,您可以用倒排表查找索引。

我觉得这个函数非常有用,它会进入我的永久“Lua实用程序”库。

2

还有另一种方法,你可以使用metamethods。 [编辑,让你删除值过于]

t = {} -- Create your table, can be called anything 
t.r_index = {} -- Holds the number value, i.e. t[1] = 'Foo' 
t.r_table = {} -- Holds the string value, i.e. t['Foo'] = 1 

mt = {} -- Create the metatable 
mt.__newindex = function (self, key, value) -- For creating the new indexes 
    if value == nil then -- If you're trying to delete an entry then 
     if tonumber(key) then -- Check if you are giving a numerical index 
      local i_value = self.r_index[key] -- get the corrosponding string index 
      self.r_index[key] = nil -- Delete 
      self.r_table[i_value] = nil 
     else -- Otherwise do the same as above, but for a given string index 
      local t_value = self.r_table[key] 
      self.r_index[t_value] = nil 
      self.r_table[key] = nil 
     end 
    else 
     table.insert(self.r_index, tonumber(key), value) -- For t[1] = 'Foo' 
     self.r_table[value] = key -- For t['Foo'] = 1 
    end 
end 
mt.__index = function (self, key) -- Gives you the values back when you index them 
    if tonumber(key) then 
     return (self.r_index[key]) -- For For t[1] = 'Foo' 
    else 
     return (self.r_table[key]) -- For t['Foo'] = 1 
    end 
end 

setmetatable(t, mt) -- Creates the metatable 

t[1] = "Rock1" -- Set the values 
t[2] = "Rock2" 

print(t[1], t[2]) -- And *should* proove that it works 
print(t['Rock1'], t['Rock2']) 

t[1] = nil 
print(t[1], t[2]) -- And *should* proove that it works 
print(t['Rock1'], t['Rock2']) 

它更灵活,你可以复制t值,并把它与你同在这也意味着你大部分时间只需要玩一个变量 - 希望能减少你尝试访问错误事物的可能性。

2

最简单的方法是将一个“索引”属性添加到每个岩石:

RockTable = {} 

for i=1,20 do 

    local rock 
    -- do your thing that generates a new 'rock' object 

    rock.index = #RockTable + 1 
    RockTable[rock.index] = rock 

end 

如果您使用的是触摸监听方法,你可以检索的岩石是这样的:

function touchListener(event) 
    local rock = event.target 
    local rockIndex = rock.index 
    -- ... 
end 

它真的,你可以用索引维护第二张表,但是我发现我的方法更加清晰 - 当需要删除东西的时候,你只需要担心一张表,即主表。

我有一个问题,但为什么你需要检索该索引?在大多数情况下,设计良好的事件监听器功能就足够了,您不需要“查找”您的对象。当然,我缺乏关于你想要做什么的信息,但有可能你过于复杂了。