如何找到 Corona SDK 显示对象的 Lua table 中的所有条目?

How to find all entries in a Lua table for Corona SDK display objects?

我关注了this tutorial in Corona SDK documentation

我正在尝试从显示对象打印所有条目和子table,以及那些子table中的条目。

在 Corona SDK 中,显示对象是 Lua table 所以我尝试了教程中列出的基本内容。

for k,v in pairs(myTable) do
    print( k,v )
end

还有一个更高级的函数应该输出所有子table,即:

local function printTable( t )

    local printTable_cache = {}

    local function sub_printTable( t, indent )

        if ( printTable_cache[tostring(t)] ) then
            print( indent .. "*" .. tostring(t) )
        else
            printTable_cache[tostring(t)] = true
            if ( type( t ) == "table" ) then
                for pos,val in pairs( t ) do
                    if ( type(val) == "table" ) then
                        print( indent .. "[" .. pos .. "] => " .. tostring( t ).. " {" )
                        sub_printTable( val, indent .. string.rep( " ", string.len(pos)+8 ) )
                        print( indent .. string.rep( " ", string.len(pos)+6 ) .. "}" )
                    elseif ( type(val) == "string" ) then
                        print( indent .. "[" .. pos .. '] => "' .. val .. '"' )
                    else
                        print( indent .. "[" .. pos .. "] => " .. tostring(val) )
                    end
                end
            else
                print( indent..tostring(t) )
            end
        end
    end

    if ( type(t) == "table" ) then
        print( tostring(t) .. " {" )
        sub_printTable( t, "  " )
        print( "}" )
    else
        sub_printTable( t, "  " )
    end
end

但是这些都没有实际打印出这些 table 中的所有条目。如果我创建一个简单的矩形并尝试使用其中一个函数,我只会得到两个 table,但我知道还有更多:

local myRectangle = display.newRect( 0, 0, 120, 40 )
for k,v in pairs(myRectangle) do
    print( k,v ) -- prints out "_class table, _proxy userdata"
end
print( myRectangle.x, myRectangle.width ) -- but this prints out "0, 120"

-- But if I were to add something to the table, then the loop finds that as well, e.g.

local myRectangle = display.newRect( 0, 0, 120, 40 )
myRectangle.secret = "hello"
for k,v in pairs(myRectangle) do
    print( k,v ) -- prints out "_class table, _proxy userdata, secret hello"
end

那么,如何打印出显示对象中包含的所有内容?很明显,这两种方法都无法获取主显示对象中的条目 table.

通常您不能,因为您可能有一个与 table 关联的元 table,它将为字段 "on the fly" 生成结果。在这种情况下,您会返回类似于 ShapeObject 的东西,它提供了 fillpathsetFillColor 等方法,但这些方法隐藏在用户数据对象后面。它还提供继承,所以有一些字段你可以查询 "belong" 到其他 类。您可以使用 pairs/ipairs 获取 table 中的键列表,这就是您在示例中得到的(设置 secret 会向 table 添加一个新键) .

关于显示对象的属性(不是方法)- Corona 中的显示对象有特殊的 属性 displayObject._properties Documentation

_properties 将 return 字符串,因此您有两种打印方法:

local myRectangle = display.newRect( 0, 0, 120, 40 )

-- Approach 1:
-- Prettify here to make better readability to json
local json = require("json")
print("myRectangle._properties: ", json.prettify( myRectangle._properties ) )

-- Approach 2:
local json = require("json")
local myRectangleProps = json.decode(myRectangle._properties) -- I skipped error handling here, because there shouldn't be any
for k,v in pairs(myRectangleProps) do
    print( k,v ) -- prints x 0, width 120, etc
end