在Lua中,如何找出存储对象的键? [英] In Lua, how do you find out the key an object is stored in?

查看:123
本文介绍了在Lua中,如何找出存储对象的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何print()或找出对象的索引?

How would you print() out or find out the index of an object?

例如,如果我在屏幕上将20个随机岩石对象生成到数组RockTable = {};

For example, if I spawned 20 random rock objects on screen into an array RockTable = {};

喜欢RockTable[#RockTable + 1] = rock;

所有20块岩石都显示在屏幕上,我如何通过单击找到每块岩石的键或索引?

And all 20 rocks are displayed on screen how would I find out what key or index each rock has by clicking on them?

我正在使用Corona SDK.

I'm using Corona SDK.

任何帮助将不胜感激.

推荐答案

反转表格:

function table_invert(t)
  local u = { }
  for k, v in pairs(t) do u[v] = k end
  return u
end

然后您可以使用倒置表查找索引.

You can then use the inverted table to find the index.

我发现此功能非常有用,以至于可以进入我的永久性"Lua实用程序"库.

I find this function so useful that it goes into my permanent "Lua utilities" libraries.

这篇关于在Lua中,如何找出存储对象的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆