【发布时间】:2014-08-04 06:26:27
【问题描述】:
我的 main.lua 将我指向第一级,我有一个 onCollision 函数将我带到第二级,但是当我进入第二级时,除了物理引擎之外,一切都加载正常。我在控制台中收到错误消息 ERROR: physical.start() has not been called。但是在 level2.lua 的顶部我已经声明:
local physics = require( "physics" )
physics.start()
我的 level1.lua 文件到此结束:
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == "will" then
-- Called when the scene is on screen and is about to move off screen
physics.stop()
elseif phase == "did" then
-- Called when the scene is now off screen
end
end
function scene:destroy( event )
-- Called prior to the removal of scene's "view" (sceneGroup)
local sceneGroup = self.view
package.loaded[physics] = nil
physics = nil
end
我的level2.lua的开头是这样的:
-----------------------------------------------------------------------------------
--
-- level2.lua
--
---------------------------------------------------------------------------------------
local composer = require( "composer" )
local scene = composer.newScene()
-- include Corona's "physics" library
local physics = require ("physics")
physics.start()
-- forward declarations and other locals
local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth*0.5, display.contentHeight*0.5
local centerY, centerX = display.contentCenterY, display.contentCenterX
即使我已经很清楚地开始了物理,为什么我会收到此错误?我尝试将完全相同的代码从 level1.lua 复制到 level2.lua,但我仍然得到相同的结果。
【问题讨论】:
-
将physics.start() 移动到您的场景中:create(event) 函数。
-
我试过了,还是不行,还是一样的错误信息。
-
可能有很多东西。也许是一个使用物理模块的听众,您没有正确关闭/从旧场景中删除?这样,当您在另一个场景中移除物理时,侦听器可能会在您没有启用物理时触发错误。但如前所述,错误可能出现在很多地方,无法在提供的代码中找到任何内容。
-
谢谢,我意识到我有一个未删除的事件监听器。我仍然收到错误,但一切正常。
标签: lua coronasdk physics-engine