2017-09-21 77 views
2

我有一組itens的在一個目的:如何從lua中的對象獲取特定物品?

frames = { 
    { name=HUD, x = 1345, y = 366, width = 426, height = 329, sourceX=0, sourceY=0, sourceWidth=426 , sourceHeight=329 }, 
    { name=background, x = 0, y = 0, width = 722, height = 1142, sourceX=0, sourceY=0, sourceWidth=722 , sourceHeight=1142 }, 
    { name=btn_ads, x = 1258, y = 1022, width = 91, height = 87, sourceX=0, sourceY=4, sourceWidth=91 , sourceHeight=91 }, 
    { name=btn_home, x = 1072, y = 1026, width = 91, height = 87, sourceX=0, sourceY=4, sourceWidth=91 , sourceHeight=91 }, 
    { name=btn_leaderboard, x = 1165, y = 1026, width = 91, height = 87, sourceX=0, sourceY=4, sourceWidth=91 , sourceHeight=91 }, 
    { name=btn_nosound, x = 1345, y = 697, width = 91, height = 87, sourceX=0, sourceY=4, sourceWidth=91 , sourceHeight=91 }, 
    { name=btn_play, x = 1202, y = 1843, width = 195, height = 196, sourceX=0, sourceY=0, sourceWidth=195 , sourceHeight=196 }, 
    { name=btn_rate, x = 1399, y = 1933, width = 91, height = 87, sourceX=0, sourceY=4, sourceWidth=91 , sourceHeight=91 }, 
    { name=btn_replay, x = 1012, y = 828, width = 196, height = 196, sourceX=0, sourceY=0, sourceWidth=196 , sourceHeight=196 }, 
    { name=btn_restore, x = 1399, y = 1843, width = 91, height = 88, sourceX=0, sourceY=3, sourceWidth=91 , sourceHeight=91 }, 
    { name=btn_share, x = 1351, y = 1022, width = 91, height = 87, sourceX=0, sourceY=4, sourceWidth=91 , sourceHeight=91 }, 
    { name=btn_sound, x = 1438, y = 697, width = 91, height = 87, sourceX=0, sourceY=4, sourceWidth=91 , sourceHeight=91 }, 
    { name=cloud, x = 723, y = 1843, width = 477, height = 174, sourceX=0, sourceY=0, sourceWidth=477 , sourceHeight=174 }, 
    { name=column, x = 1507, y = 1173, width = 51, height = 612, sourceX=0, sourceY=0, sourceWidth=51 , sourceHeight=612 }, 
    { name=floor, x = 0, y = 1843, width = 721, height = 168, sourceX=0, sourceY=0, sourceWidth=721 , sourceHeight=168 }, 
    { name=game_over, x = 724, y = 366, width = 619, height = 229, sourceX=51, sourceY=9, sourceWidth=721 , sourceHeight=251 }, 
    { name=lava_1, x = 0, y = 1144, width = 721, height = 312, sourceX=0, sourceY=0, sourceWidth=721 , sourceHeight=312 }, 
    { name=lava_2, x = 723, y = 1144, width = 721, height = 312, sourceX=0, sourceY=0, sourceWidth=721 , sourceHeight=312 }, 
    { name=lava_3, x = 0, y = 1458, width = 721, height = 309, sourceX=0, sourceY=3, sourceWidth=721 , sourceHeight=312 }, 
    { name=logo, x = 724, y = 597, width = 619, height = 229, sourceX=52, sourceY=9, sourceWidth=721 , sourceHeight=251 }, 
    { name=needle, x = 1446, y = 1494, width = 11, height = 84, sourceX=0, sourceY=0, sourceWidth=11 , sourceHeight=84 }, 
    { name=new_score, x = 1210, y = 960, width = 46, height = 46, sourceX=2, sourceY=0, sourceWidth=51 , sourceHeight=51 }, 
    { name=object_1, x = 724, y = 993, width = 209, height = 99, sourceX=0, sourceY=0, sourceWidth=209 , sourceHeight=99 }, 
    { name=object_2, x = 1446, y = 1022, width = 89, height = 149, sourceX=0, sourceY=0, sourceWidth=89 , sourceHeight=149 }, 
    { name=object_3, x = 935, y = 1026, width = 135, height = 108, sourceX=0, sourceY=0, sourceWidth=135 , sourceHeight=108 }, 
    { name=object_4, x = 1340, y = 828, width = 113, height = 192, sourceX=0, sourceY=0, sourceWidth=113 , sourceHeight=192 }, 
    { name=object_5, x = 1210, y = 828, width = 128, height = 130, sourceX=0, sourceY=0, sourceWidth=128 , sourceHeight=130 }, 
    { name=parallax1_1, x = 723, y = 1458, width = 721, height = 383, sourceX=0, sourceY=0, sourceWidth=721 , sourceHeight=383 }, 
    { name=parallax1_2, x = 724, y = 0, width = 721, height = 364, sourceX=0, sourceY=0, sourceWidth=721 , sourceHeight=364 }, 
    { name=player_1, x = 1446, y = 1350, width = 59, height = 142, sourceX=0, sourceY=0, sourceWidth=59 , sourceHeight=175 }, 
    { name=player_2, x = 1446, y = 1173, width = 59, height = 175, sourceX=0, sourceY=0, sourceWidth=59 , sourceHeight=175 }, 
    { name=tip, x = 724, y = 828, width = 286, height = 163, sourceX=0, sourceY=0, sourceWidth=286 , sourceHeight=163 } 
} 

我有一個名字,並希望使用該名稱來引用對象,例如:

self.frames['logo']

拉出:

{ name=logo, x = 724, y = 597, width = 619, height = 229, sourceX=52, sourceY=9, sourceWidth=721 , sourceHeight=251 },

這可能嗎?

從我可以收集,唯一的方法來做到這一點將循環通過對象中的每個項目,直到我找到它?

+0

沒有任何其他預處理,你是正確的。你需要循環。但是如果你可以創建一個以鍵爲名的新表,它將會是不變的時間查詢。 –

回答

3

如果對象的順序沒有特別的,重要的是,你可以把它們存儲在表中的散列部:

frames = { 
    HUD = { name="HUD", x = 1345, y = 366, width = 426, height = 329, sourceX=0, sourceY=0, sourceWidth=426 , sourceHeight=329 }, 
    background = { name="background", x = 0, y = 0, width = 722, height = 1142, sourceX=0, sourceY=0, sourceWidth=722 , sourceHeight=1142 }, 
    btn_ads = { name="btn_ads", x = 1258, y = 1022, width = 91, height = 87, sourceX=0, sourceY=4, sourceWidth=91 , sourceHeight=91 }, 
    -- ... 
} 

然後你就可以用frames.HUDframes["HUD"]等訪問它們。

+0

您可能需要'name =「HUD」'或更好地遍歷表並根據其鍵設置內部表的'name'字段。 – lhf