2017-09-21 6 views
2

je un ensemble de itens dans un objet:Comment puis-je obtenir un objet spécifique d'un objet dans 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 } 
} 

J'ai un nom et je voudrais faire référence à l'objet en utilisant ce nom, par exemple:

self.frames['logo']

Pour sortir:

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

Est-ce possible? D'après ce que je peux rassembler, la seule façon de le faire serait de faire une boucle sur chaque élément de l'objet jusqu'à ce que je le trouve?

+0

Sans aucun autre pré-traitement, vous avez raison. Vous devez passer en boucle. Mais si vous pouvez créer une nouvelle table avec des clés comme nom, ce sera la recherche de temps constant. –

Répondre

3

Si l'ordre des objets est pas particulièrement important, vous pouvez les stocker dans la partie de hachage de la table:

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

Ensuite, vous pouvez y accéder avec frames.HUD ou frames["HUD"] et ainsi de suite.

+0

Vous avez probablement besoin de 'name =" HUD "' ou mieux de parcourir la table et de définir le champ 'name' des tables internes en fonction de leurs clés. – lhf