2017-09-21 35 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