如何从lua中的对象获取特定项?

时间:2017-09-21 19:27:02

标签: lua

我在一个对象中有一组itens:

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 },

这可能吗?

从我可以收集到的,唯一的方法是循环遍历对象中的每个项目,直到找到它为止?

1 个答案:

答案 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"]访问它们,依此类推。