【问题标题】:Corona SDK / Lua : An table's property is nil, when accessed via event.other during collision event. But why?Corona SDK / Lua:在碰撞事件期间通过 event.other 访问时,表的属性为零。但为什么?
【发布时间】:2017-08-17 14:20:31
【问题描述】:

所以我有这个模块,它在游戏期间的所有活动都在其中。在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

【问题讨论】:

  • 您将event.other 作为第三个参数传递给函数t.grow,但它从未在内部使用(函数内部的本地名称是hero)。
  • hero.col == nilt.grow() 中吗?
  • 我在 n 答案中解释了错误。另外,对不起,我忘记包含 nil 指针,我在发布之前无意中删除了它。

标签: lua null coronasdk collision


【解决方案1】:

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

谢谢你的回答!

【讨论】:

  • 很高兴你知道了。您是否也可以通过单击我的答案旁边的向上箭头来表示感谢:-)
  • 是的,我已经这样做了,但由于我的声誉低,它没有显示。
  • 谢谢。别担心。你的声望会迅速增加。
【解决方案2】:

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

来自Collision Detection 上的 Corona 文档:

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

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

【讨论】:

  • 我在 n 答案中解释了错误。另外,对不起,我忘了包含 nil 指针,我在发布之前无意中删除了它。
  • 还有: hero 是动态体(其他模块)。
猜你喜欢
  • 2014-03-14
  • 1970-01-01
  • 2017-08-24
  • 2015-10-11
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 2015-02-09
相关资源
最近更新 更多