【问题标题】:Changing scene on collision - cannot remove-scene() perspective.lua: attempt to compare nil with number碰撞时更改场景 - 无法删除场景() perspective.lua:尝试将 nil 与数字进行比较
【发布时间】:2014-12-08 16:51:10
【问题描述】:

当与物体发生碰撞时,我正在尝试改变场景。仅供参考,当我在按钮上使用监听器时,效果很好!场景打扫干净了,“物理”也没有问题了。

场景:CH1-SAH-A01

    function onCollision(event)
        if (event.phase=="began") then
            if (event.object1.myName=="hero" and event.object2.myName=="start_point") then              
                composer.gotoScene("CH1-SPR-A01", "fade", 500)
            end
        elseif (event.phase=="ended") then
            if (event.object1.myName=="hero" and event.object2.myName=="ground") then   
            end
         end
    end

我从这里开始物理:

function scene:show( event )

local sceneGroup = self.view
local phase = event.phase

if ( phase == "will" ) then
      -- Called when the scene is still off screen (but is about to come on screen).
    camera:setBounds(display.contentWidth/2,4200-display.contentWidth*0.5,display.contentHeight-1500,-220)
    camera.damping=1
    physics.start()
    camera:track()
    setHeroPropreties()

然后,当场景离开时:

function scene:hide( event )
  local sceneGroup = self.view
  local phase = event.phase    
   if ( phase == "will" ) then        
    timer.performWithDelay( 1000, function() physics.stop();end )
    Runtime:removeEventListener("enterFrame", ShowCharacters)
    Runtime:removeEventListener("collision", onCollision)
    slideOffSensor:removeEventListener( "touch", handleSlideOff )
    composer.removeScene("CH1-SAH-A01")
   elseif ( phase == "did" ) then          

   end
end

销毁函数是:

function scene:destroy( event )

    local sceneGroup = self.view
    local camera = self.view
    package.loaded[physics] = nil
    physics = nil
end

我试图在函数physics.stop() 中添加一个计时器,但徒劳无功。碰撞后,虚拟控制台监视器出现了一些“错误”:

我在当前和目标场景中没有使用 transition.to(),我有这个错误:

错误:在解决冲突之前无法翻译对象

我在我的“英雄”和 start_point(在目标场景中)的 Physics.addbody 中添加了一个计时器,但仍然出现此错误:

错误:当世界被锁定时,physics.addBody() 不能被调用 在数字运算的中间,例如在碰撞事件期间

有什么建议吗?

【问题讨论】:

  • 抛出的lua错误是哪一行。这只是意味着将 nil 的值与数字 (?) 进行比较。

标签: lua coronasdk


【解决方案1】:

在电晕中,在碰撞结束之前,您无法更改与碰撞有关的物体的任何内容。此外,当碰撞正在进行时,您不能调用physics.stop()。您需要使用一个标志或一个带有 timer.performWithDelay() 的延迟,直到碰撞结束,然后再更改场景。

此链接的“重要”部分对此进行了介绍: http://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html

【讨论】:

    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2021-03-24
    • 2018-11-12
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多