【问题标题】:How to find all entries in a Lua table for Corona SDK display objects?如何在 Lua 表中查找 Corona SDK 显示对象的所有条目?
【发布时间】:2020-04-06 11:25:51
【问题描述】:

我关注了this tutorial in Corona SDK documentation

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

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

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

还有一个更奇葩的函数应该输出所有子表,即:

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

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

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

那么,如何打印显示对象中包含的所有内容?显然这两种方法都没有得到主显示对象表中的条目。

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    一般情况下您不能,因为您可能有一个与该表关联的元表,它会“即时”为字段生成结果。在这种情况下,您会得到类似 ShapeObject 的东西,它提供了 fillpathsetFillColor 等方法,但这些方法隐藏在 userdata 对象后面。它还提供继承,因此您可以查询一些“属于”其他类的字段。您可以使用pairs/ipairs 来获取表中的键列表,这就是您在示例中得到的(设置secret 会向表中添加一个新键)。

    【讨论】:

      【解决方案2】:

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

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

      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
      

      【讨论】:

      • 这是获取更多信息的一种方式,但它并不能揭示一切。它仅揭示了 Corona 开发人员认为您可能需要的特定值。大多数东西仍然隐藏在底层,例如物理实体、顶点或填充相关信息。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      相关资源
      最近更新 更多