加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

lua – event.other在使用故事板库[CORONA sdk]恢复场景后在onCo

发布时间:2020-12-14 21:44:19 所属栏目:大数据 来源:网络整理
导读:我在使用与碰撞检测和故事板库相关的Corona SDK时遇到了一个奇怪的错误. 这是我的onCollision监听器: local function onBottomBorderCollision( event ) if (event.other.isBall) then local index = table.indexOf( ballArray,other ) table.remove( ballA
我在使用与碰撞检测和故事板库相关的Corona SDK时遇到了一个奇怪的错误.

这是我的onCollision监听器:

local function onBottomBorderCollision( event )    
    if (event.other.isBall) then
        local index = table.indexOf( ballArray,other )
        table.remove( ballArray,index )
        event.other:removeSelf( )
        event.other = nil
        updateLife(false)
        updateScore(false)
    end
end

这在第一次启动时工作正常,但在返回菜单屏幕(使用storyboard.goToScene(“menu”))并重放游戏后,现在每次我的一个球击中底部边框时,此侦听器将触发以下错误:

attempt to index field “other”(a nil value)

我确实在场景中创建了正确的侦听器:onEnterScene(场景)所以它与它们无关,而且这个其他侦听器永远不会生成错误:

local function onPlayerCollision( event )


    if(event.other.isBall) then
       xVel,yVel = event.other:getLinearVelocity( )
       event.other:setLinearVelocity( event.other.XLinearVelocity,yVel )

   end

end

我现在被困住了……请帮忙!

解决方法

实际上,这种类型的错误主要是由于一些活动的函数调用/定时器/转换引起的,在更改场景之前尚未取消.

attempt to index field "other"(a nil value)

上述错误提到正在调用/获取任何对象/它的属性,但它不在当前事件或场景中.因此,请检查您是否取消了计时器,转换和运行时事件侦听器.

在您的情况下,可能是由于在运行时未能取消onBottomBorderCollision上的碰撞检测功能.如果您在enterFrame中调用它,则必须在场景更改之前取消它.你可以这样做:

Runtime:removeEventListener("enterFrame",onBottomBorderCollision)

更新:在碰撞检查运行时,您无法停止物理引擎.所以做如下:

function actualSceneChange()
     physics.stop() -- stop physics here
     -- call scene chage
     director:changeScene("menuPageName") -- call your scene change method
end

function initiatingSceneChange()
    physics.pause() -- pause physics here
    -- stop unwanted event listeners
    Runtime:removeEventListener("enterFrame",onBottomBorderCollision) 

    if(timer_1)then timer.cancel(timer_1) end    -- stop all your timers like this.
    if(trans_1)then transition.cancel(trans) end -- stop all your transitions like this.

    timer.performWithDelay(1000,actualSceneChange,1)
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读