2012-03-17 65 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 

它真的,你可以用索引維護第二張表,但是我發現我的方法更加清晰 - 當需要刪除東西的時候,你只需要擔心一張表,即主表。

我有一個問題,但爲什麼你需要檢索該索引?在大多數情況下,設計良好的事件監聽器功能就足夠了,您不需要「查找」您的對象。當然,我缺乏關於你想要做什麼的信息,但有可能你過於複雜了。

相關問題