Corona SDK / Lua:在碰撞事件期间通过 event.other 访问时,table 的 属性 为零。但为什么?

Corona SDK / Lua : An table's property is nil, when accessed via event.other during collision event. But why?

所以我有这个模块,它在游戏中的所有 activity 都在里面。在 t.physics 我添加了一个碰撞事件侦听器(区分目标是一组还是单个对象) .但是,当相关对象检测到碰撞时,另一个对象 (event.other) 的 属性 col 似乎是 nil,尽管我最初将其设置为表示 a 的字符串t.create 中的颜色。我只是找不到原因,有人可以吗?

感谢您的帮助。 你好,尼尔斯

local fence = require("lib.fence")
local physics = require("physics")
local t = {}
    local stages = {yellow = 1, lila = 1, red = 1}
    local sizes = {1, 3.625, 7.25}
    t.colors = {"yellow", "lila", "red"}
    t.growing = false

    t.setSize = function(fill)
        local tHeight = fill.contentHeight * sizes[stages[fill.col]]
        local tScale = tHeight / fill.contentHeight
        fill.yScale = tScale

    end

    t.grow = function(group, color, hero)
        local counter = 0
        stages[color] = stages[color] + 1
        for i = 1, group.numChildren, 1 do
            if group[i].col == color then
                counter = counter + 1
                local function newPhysics() t.physics(group) end
                if counter == 1 then
                    local function reset() t.growing = false if stages[color] == 3 then stages[color] = 1; newPhysics(); end end
                    local function start() t.growing = true end
                    transition.to(group[i], {time = 260, yScale = sizes[stages[color]], onStart = start, onComplete = reset})
                else
                    transition.to(group[i], {time = 250, yScale = sizes[stages[color]], onStart = start})
                end
            end
        end
    end

    t.physics = function(target)
        if target.numChildren == nil then
            physics.removeBody(target)
            local function add()
                physics.addBody( target, "static", {isSensor = true} )
                target.collision = function(self, event)
                    if event.phase == "began" then
                        target.count = target.count + 1
                        if target.count == 1 then
                            t.grow(target.parent, self.col, event.other)
                        end
                    elseif event.phase == "ended" then
                        target.count = 0
                    end
                end
            end
            timer.performWithDelay(1, add, 1)
        else
            for i = 1, target.numChildren, 1 do
                physics.removeBody( target[i] )
                physics.addBody( target[i], "static", {isSensor = true} )
                target[i].name = "fill"
                local fill = target[i]
                fill.count = 0
                fill.collision = function(self, event)
                    if event.phase == "began" then
                        self.count = self.count + 1
                        if self.count == 1 and event.other.x ~= nil then
                            t.grow(target, self.col, event.other)
                        end
                    else
                        fill.count = 0
                    end
                end
                fill:addEventListener("collision")
            end
        end
    end

    t.setColor = function(fill)
        local colors = {
            {238 / 255, 228 / 255, 28 / 255},
            {38 / 255, 33 / 255, 77 / 255},
            {175 / 255, 24 / 255, 52 / 255},
        }
        local names = {"yellow", "lila", "red"}
        local r = math.random(3)
        fill.fill = colors[r]
        fill.col = names[r]
    end

    t.create = function(fences, group, colors)
        local fills = {}
        for i = 1, #fences, 1 do
            local rCol = math.random(3)
            local col
            if rCol == 1 then
                col = colors.yellow
            elseif rCol == 2 then
                col = colors.lila
            else
                col = colors.red
            end
            fills[i] = display.newRect(
                group, fences[i].x + fences[i].contentWidth * 0.125, fences[i].y,
                fences[i].contentWidth * 0.9, (fences[i].contentHeight * 0.5 / 3)
            )
            fills[i].dPosX = fills[i].x
            fills[i].y = display.contentHeight- fills[i].contentHeight / 2
            fills[i].fill = col
            fills[i].col = t.colors[rCol]
            fills[i].increased = false
        end
        return fills
    end

    t.move = function(fills, fences, group)
        for i = 1, #fills, 1 do
            local fill = fills[i]
            function fill:enterFrame()
                self:translate(fence.speed, 0)
                if t.growing == false then
                    t.setSize(self)
                end
                if self.x > display.contentWidth + 0.55 * fences[i].contentWidth then
                    local xT = {}
                    for i = 1, group.numChildren, 1 do
                        xT[i] = group[i].x
                    end
                    local function compare(a, b) return a < b end
                    table.sort(xT, compare)
                    self.x = xT[1] - fences[i].contentWidth * 0.98
                    t.setColor(self)
                    local function newPhysics() t.physics(self) end
                    timer.performWithDelay( 25, newPhysics, 1 )
                    self:toBack()
                end
            end
            Runtime:addEventListener("enterFrame", fill)
        end
    end
return t

你这里好像没有动态物体。什么与什么相撞?会不会是碰撞中涉及的另一个对象(event.other 的值)不是在 t.create() 中初始化的东西,所以没有 col 属性?

来自 Collision Detection 上的 Corona 文档:

Some body types will — or will not — collide with other body types. In a collision between two physical objects, at least one of the objects must be dynamic, since this is the only body type which collides with any other type.

此外,在您的 fill.collision() 中,我认为您希望将 event.target 作为第一个参数传递给 t.grow() 而不是目标。如果您尝试,请使用更多信息更新问题。

已解决。呃,抱歉,我忘了在另一个涉及的对象(英雄)上定义 属性,它有自己的模块。多么愚蠢的失误。

无论如何感谢您的回答!