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



所以我有这个模块,它在游戏期间的所有活动都在其中。t.physics我添加了一个碰撞事件侦听器(区分目标是组还是单个对象(。当相关对象检测到碰撞时,另一个对象(event.other(的属性col似乎nil,尽管我最初将其设置为表示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

已解决。呃,对不起,我忘了在涉及的其他对象(hero(上定义属性,该对象有自己的模块。多么愚蠢的失误。

无论如何,感谢您的回答!

你在这里似乎没有任何动态的身体。 什么与什么碰撞? 是否涉及碰撞的另一个对象(event.other的值(不是在t.create()中初始化的东西,因此没有col属性?

来自关于碰撞检测的 Corona 文档:

一些体型会 - 或不会 - 与其他体型发生碰撞。在两个物理对象之间的碰撞中,至少其中一个对象必须是动态的,因为这是唯一与任何其他类型碰撞的体型。

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

相关内容

  • 没有找到相关文章

最新更新