【问题标题】:Code is running fine but corona simulator freezes at a particular scene代码运行良好,但电晕模拟器冻结在特定场景
【发布时间】:2014-03-31 08:43:25
【问题描述】:

我有一个应用程序在第 9 个场景中一直卡住。我目前正在使用故事板,想法是在玩家回答问题时进入场景。我使用打印语句来确认电晕已进入第 10 个场景(所有代码都在场景 10 中工作并运行),但模拟器在场景 9 处冻结。我尝试将场景 9 链接到场景 1,这也是相同的结果,模拟器屏幕在场景 9 冻结,而不是转到场景 1,但场景 1 中的打印语句显示在模拟器上。这些代码可以正常工作,因为我在每个函数中都有打印语句。我取出了一些代码并更改了文件的名称,但没有运气。我还能看什么?

我还在手机上运行了代码,发现了一些似乎有些错误:

D/CrashAnrDetector( 2962): Hardware: universal5410
D/CrashAnrDetector( 2962): Revision: 10
D/CrashAnrDetector( 2962): Bootloader: I9500ZSUCMK3
D/CrashAnrDetector( 2962): Radio: unknown
D/CrashAnrDetector( 2962): Kernel: Linux version 3.4.5-2138764 (se.infra@R0304-0
8) (gcc version 4.6.x-google 20120106 (prerelease) (GCC) ) #1 SMP PREEMPT Fri No
v 15 16:58:07 KST 2013

然后是更多的崩溃检测器,后面有一堆数字。

D/CrashAnrDetector( 2962):     d0  65746172656e6567  d1  7571207478656e20
D/CrashAnrDetector( 2962):     d2  0000000000000000  d3  0000000000000000
D/CrashAnrDetector( 2962):     d4  0000000000000000  d5  3ff0000000000000
D/CrashAnrDetector( 2962):     d6  c00ffffe00000000  d7  c07ffff000000000
D/CrashAnrDetector( 2962):     d8  40400030c07ffff0  d9  000000003f800000
D/CrashAnrDetector( 2962):     d10 000000003f7fffec  d11 0000000000000000
.....

E/android.os.Debug( 2962): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dump
state_app_error
I/dumpstate(29372): begin
I/dumpstate(29372): dumpstate is still running
....

我可以从中看出应用程序崩溃并且存储问题似乎有问题?(我认为这就是 dumpstate 所指的?)

local storyboard = require( "storyboard" )
local widget = require "widget"
local scene = storyboard.newScene()

local function returnHome()
storyboard.gotoScene("scene_menu", "crossFade",1000)

return true
end

local physics = require('physics')
physics.start()

-- Clear previous scene


-- local forward references should go here --
local score = 0
local scoreText
local scoreForLevelComplete
local background
local infoBar
local restartBtn
local cannon
local levelNum
local balloon = {}
local cannonCharge = {}
local ballCollision = {}
local shot = {}
local cannonBall
local impulse = 0
local balloons = {}
local cannonCharge = {}
local shot = {}
local cannonBalls = display.newGroup()
local pop = audio.loadSound('pop.mp3')
local wrongAnswer


---------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------




function scene:createScene( event )
local group = self.view
storyboard.removeAll()
print('purge scene')
  end

function scene:enterScene(event)
 local group = self.view

background = display.newImage( "bkg_clouds.png")
group:insert(background)
background.x = 230
background.y = 195

scoreText = display.newText( storyboard.state.score, 0, 0, native.systemFont, 32 )
scoreText:setFillColor( 0,0, 0 )
scoreText.x = 87
scoreText.y = 28
group:insert( scoreText ) 

questionText = display.newText('a', display.contentCenterX, display.contentWidth/4, native.systemFont, 40)
group:insert(questionText)

infoBar = display.newImage ("infoBar.png")
group:insert(infoBar)
infoBar.x = 10
infoBar.y = 25

restartBtn = widget.newButton{
defaultFile = "restartBtn.png",
onRelease = returnHome
}
restartBtn.x = 470
restartBtn.y = 300
group:insert(restartBtn)


cannon = display.newImage ("cannon.png")
group:insert(cannon)
cannon.x = 10
cannon.y = 270

cannon.anchorX = 0.5
cannon.anchorY = 0.5
restartBtn.isVisible = true

local MemoryLabel = display.newText( 
    { 
        text = '',
        x = 250,
        y = 220,
        font = 'Arial',
        fontSize = 32,
        align = "center"
    }
)
MemoryLabel:setFillColor( 0 )
group:insert(MemoryLabel)

local TextureLabel = display.newText( 
    { 
        text = '',
        x = 250,
        y = 200,
        font = 'Arial',
        fontSize = 32,
        align = "center"
    }
)
TextureLabel:setFillColor( 0 )
group:insert(TextureLabel)

-- To check available memory
function checkMemory()
    collectgarbage( "collect" )
    MemoryLabel.text = string.format( "M:%.2f KB", collectgarbage( "count" ) )
    TextureLabel.text = "T:"..(string.format( "%.2f KB", system.getInfo("textureMemoryUsed") / (1024 * 1024)) )

end

timer.performWithDelay( 1000, checkMemory, 0 )


local balloon1 = display.newImage ('balloon_fat_red.png', 495, 70)
local balloon2 = display.newImage ('balloon_fat_red.png', 495, 115)
local balloon3 = display.newImage ('balloon_fat_red.png', 495, 160)

group:insert(balloon1)
group:insert(balloon2)
group:insert(balloon3)

physics.addBody(balloon1)
physics.addBody(balloon2)
physics.addBody(balloon3)

balloon1.bodyType = 'static'
balloon2.bodyType = 'static'
balloon3.bodyType = 'static'

table.insert(balloons, balloon1)      
table.insert(balloons, balloon2)      
table.insert(balloons, balloon3) 

local balloonText1 = display.newText('\227\129\130', 495, 70)
balloonText1:setFillColor( 1,1, 0 )
local balloonText2 = display.newText('\227\129\132', 495, 115)
balloonText2:setFillColor( 1,1, 0 )
local balloonText3 = display.newText('\227\129\134', 495, 160)
balloonText3:setFillColor( 1,1, 0 )


group:insert(balloonText1)
group:insert(balloonText2)
group:insert(balloonText3)

function cannonCharge:touch(event)
  if(event.phase == 'began') then
        impulse = 0
        cannon.isVisible = true
        Runtime:addEventListener('enterFrame', charge)
        print ('cannonCharge')
    end
end

function shot:touch(event)
    if(event.phase == 'ended') then

        Runtime:removeEventListener('enterFrame', charge)
        cannon.isVisible = true
        cannon.rotation = 0

        cannonBall = display.newImage('cannon ball.png', 84, 220)
        physics.addBody(cannonBall, {density = 1, friction = 0, bounce = 0})
        group:insert(cannonBall)
        print ('shot')

-- Shoot cannon ball
cannonBall:applyLinearImpulse(3, impulse, cannonBall.x, cannonBall.y )

--Collision listener
print('event listener')
cannonBall:addEventListener ('collision', ballCollision)

    end
end

function charge()
local degreesPerFrame = 1
cannon.rotation = cannon.rotation - degreesPerFrame 
impulse=impulse-0.2

     if(cannon.rotation < -46) then
          cannon.rotation = -46
          impulse = -3.2
          print('Charge')
        end
end


function ballCollision(event)
   if (event.other == balloons[1])  then
        scene.updateScore()
        print('Ball is colliding')
        balloon1:removeSelf()
        balloon1 = nil
        balloonText1:removeSelf() 
        balloonText1 = nil
        local popSound = audio.play(pop)
        print('Before changing scene')
        storyboard.gotoScene("correct1", "fade", 3000)
  else
   if (event.other == balloons[2])  then
        print('Ball is colliding')
        balloon2:removeSelf()
        balloon2 = nil
        balloonText2:removeSelf() 
        balloonText2 = nil
        local popSound = audio.play(pop)
        print('Before changing scene')
        storyboard.gotoScene("wrong1", "fade", 3000)
  else
   if (event.other == balloons[3])  then
        print('Ball is colliding')
        balloon3:removeSelf()
        balloon3 = nil
        balloonText3:removeSelf() 
        balloonText3 = nil
        local popSound = audio.play(pop)
        print('Before changing scene')
        storyboard.gotoScene("wrong1", "fade", 3000)
    end
    end
    end
end

background:addEventListener('touch', cannonCharge)
background:addEventListener('touch', shot) 
end

function scene.updateScore()
  storyboard.state.score = storyboard.state.score + 50
  scoreText.text = storyboard.state.score
end

-- Called when the scene's view does not exist:


function scene:exitScene(event)
local group = self.view

background:removeEventListener('touch', cannonCharge)
background:removeEventListener('touch', shot)


end

function scene:destroyScene(event)

audio.stop(popSound)
audio.dispose(pop)
popSound = nil
pop = nil
--cannonBall:removeEventListener ('collision', ballCollision)

if restartBtn then
restartBtn:removeSelf()
restartBtn = nil


end

end

scene:addEventListener("createScene", scene)
scene:addEventListener("updateScore", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)

return scene

所以这就进入下一个场景

local storyboard = require( "storyboard" )
local widget = require "widget"
local scene = storyboard.newScene()

local function returnHome()
storyboard.gotoScene("scene_menu", "crossFade",1000)

return true
end

local physics = require('physics')
physics.start()

-- Clear previous scene
storyboard.removeAll()

-- local forward references should go here --

local scoreText
local background
local infoBar
local restartBtn
local cannon
local balloon = {}
local cannonCharge = {}
local ballCollision = {}
local shot = {}
local cannonBall
local impulse = 0
local balloons = {}
local cannonCharge = {}
local shot = {}
local cannonBalls = display.newGroup()
local pop = audio.loadSound('pop.mp3')
local wrongAnswer


---------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------




function scene:createScene( event )
local group = self.view
storyboard.removeAll()
print('purge scene')

  end

function scene:enterScene(event)
 local group = self.view

background = display.newImage( "bkg_clouds.png")
group:insert(background)
background.x = 230
background.y = 195

local cannon = display.newImage ("cannon.png")
group:insert(cannon)
cannon.x = 10
cannon.y = 270

scoreText = display.newText( storyboard.state.score, 0, 0, native.systemFont, 32 )
scoreText:setFillColor( 0,0, 0 )
scoreText.x = 87
scoreText.y = 28
group:insert( scoreText ) 

questionText = display.newText('i', display.contentCenterX, display.contentWidth/4, native.systemFont, 40)
group:insert(questionText)

infoBar = display.newImage ("infoBar.png")
group:insert(infoBar)
infoBar.x = 10
infoBar.y = 25

restartBtn = widget.newButton{
defaultFile = "restartBtn.png",
onRelease = returnHome
}
restartBtn.x = 470
restartBtn.y = 300
group:insert(restartBtn)


cannon = display.newImage ("cannon.png")
group:insert(cannon)
cannon.x = 10
cannon.y = 270

cannon.anchorX = 0.5
cannon.anchorY = 0.5
restartBtn.isVisible = true

local MemoryLabel = display.newText( 
    { 
        text = '',
        x = 250,
        y = 220,
        font = 'Arial',
        fontSize = 32,
        align = "center"
    }
)
MemoryLabel:setFillColor( 0 )
group:insert(MemoryLabel)

local TextureLabel = display.newText( 
    { 
        text = '',
        x = 250,
        y = 200,
        font = 'Arial',
        fontSize = 32,
        align = "center"
    }
)
TextureLabel:setFillColor( 0 )
group:insert(TextureLabel)

-- To check available memory
function checkMemory()
    collectgarbage( "collect" )
    MemoryLabel.text = string.format( "M:%.2f KB", collectgarbage( "count" ) )
    TextureLabel.text = "T:"..(string.format( "%.2f KB", system.getInfo("textureMemoryUsed") / (1024 * 1024)) )

end

timer.performWithDelay( 1000, checkMemory, 0 )

local balloon1 = display.newImage ('balloon_fat_red.png', 495, 70)
local balloon2 = display.newImage ('balloon_fat_red.png', 495, 115)
local balloon3 = display.newImage ('balloon_fat_red.png', 495, 160)

group:insert(balloon1)
group:insert(balloon2)
group:insert(balloon3)

physics.addBody(balloon1)
physics.addBody(balloon2)
physics.addBody(balloon3)

balloon1.bodyType = 'static'
balloon2.bodyType = 'static'
balloon3.bodyType = 'static'

table.insert(balloons, balloon1)      
table.insert(balloons, balloon2)      
table.insert(balloons, balloon3) 

local balloonText1 = display.newText('\227\129\141', 495, 70)
balloonText1:setFillColor( 1,1, 0 )
local balloonText2 = display.newText('\227\129\138', 495, 115)
balloonText2:setFillColor( 1,1, 0 )
local balloonText3 = display.newText('\227\129\132', 495, 160)
balloonText3:setFillColor( 1,1, 0 )


group:insert(balloonText1)
group:insert(balloonText2)
group:insert(balloonText3)

function cannonCharge:touch(event)
  if(event.phase == 'began') then
        impulse = 0
        cannon.isVisible = true
        Runtime:addEventListener('enterFrame', charge)
        print ('cannonCharge')
    end
end

function shot:touch(event)
    if(event.phase == 'ended') then

        Runtime:removeEventListener('enterFrame', charge)
        cannon.isVisible = true
        cannon.rotation = 0

        cannonBall = display.newImage('cannon ball.png', 84, 220)
        physics.addBody(cannonBall, {density = 1, friction = 0, bounce = 0})
        group:insert(cannonBall)
        print ('shot')

-- Shoot cannon ball
cannonBall:applyLinearImpulse(3, impulse, cannonBall.x, cannonBall.y )

--Collision listener
print('event listener')
cannonBall:addEventListener ('collision', ballCollision)

    end
end

function charge()
local degreesPerFrame = 1
cannon.rotation = cannon.rotation - degreesPerFrame 
impulse=impulse-0.2

     if(cannon.rotation < -46) then
          cannon.rotation = -46
          impulse = -3.2
          print('Charge')
        end
end


function ballCollision(event)
   if (event.other == balloons[1])  then
        print('Ball is colliding')
        balloon1:removeSelf()
        balloon1 = nil
        balloon2:removeSelf()
        balloon2 = nil
        balloon3:removeSelf()
        balloon3 = nil


        balloonText1:removeSelf() 
        balloonText1 = nil
        balloonText2:removeSelf() 
        balloonText2 = nil
        balloonText3:removeSelf() 
        balloonText3 = nil
        local popSound = audio.play(pop)
        storyboard.gotoScene("Question3", "fade", 1000)
  else
   if (event.other == balloons[2])  then
        print('Ball is colliding')
        balloon1:removeSelf()
        balloon1 = nil
        balloon2:removeSelf()
        balloon2 = nil
        balloon3:removeSelf()
        balloon3 = nil


        balloonText1:removeSelf() 
        balloonText1 = nil
        balloonText2:removeSelf() 
        balloonText2 = nil
        balloonText3:removeSelf() 
        balloonText3 = nil

        local popSound = audio.play(pop)
        storyboard.gotoScene("Question3", "fade", 1000)
  else
   if (event.other == balloons[3])  then
        scene.updateScore()
        print('Ball is colliding')
        balloon1:removeSelf()
        balloon1 = nil
        balloon2:removeSelf()
        balloon2 = nil
        balloon3:removeSelf()
        balloon3 = nil


        balloonText1:removeSelf() 
        balloonText1 = nil
        balloonText2:removeSelf() 
        balloonText2 = nil
        balloonText3:removeSelf() 
        balloonText3 = nil
        local popSound = audio.play(pop)
        storyboard.gotoScene("Question3", "fade", 1000)
    end
    end
    end
end

background:addEventListener('touch', cannonCharge)
background:addEventListener('touch', shot) 
end

function scene.updateScore()
  storyboard.state.score = storyboard.state.score + 50
  scoreText.text = storyboard.state.score
end

-- Called when the scene's view does not exist:


function scene:exitScene(event)
local group = self.view

cannonBall:removeSelf()
cannonBall = nill

audio.stop(popSound)
audio.dispose(pop)
popSound = nil
pop = nil

background:removeEventListener('touch', cannonCharge)
background:removeEventListener('touch', shot)
physics.stop()
end

function scene:destroyScene(event)


if restartBtn then
restartBtn:removeSelf()
restartBtn = nil
end

end

scene:addEventListener("createScene", scene)
scene:addEventListener("updateScore", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)

return scene

【问题讨论】:

  • 您要移除在场景 9.. 中创建的所有对象吗?
  • 是的,我在每个代码文件的 createScene 开头都有一个 storyboard.removeAll(),我认为它会删除对象、模块等。
  • 这可能有几个原因。如果您将应用设置为从主场景到场景 10 到场景 9 到场景 1(中间没有其他场景),它仍然会冻结吗?
  • 我按照你说的做了,但发生了一些奇怪的事情。更改后,我第一次运行代码并且它工作,除了场景 1 的音频不起作用,但它从场景 1 转到结果场景。当我第二次运行它时,它不再工作并在模拟器上的场景 1 屏幕上冻结。
  • 当我运行场景 1,2,3,4 并返回场景 1 时,碰撞侦听器突然不起作用,但它在一开始就起作用,是否有任何特殊原因会这样做?

标签: lua coronasdk


【解决方案1】:

真的很难解决,但我会尝试间接回答。

您能否尝试像这样直接在 corona 中添加内存调试功能:

oMemoryLabel = display.newText( 
    { 
        text = '',
        x = display.contentWidth*0.33,
        y = display.contentHeiht*0.33,
        font = 'Arial',
        fontSize = 32,
        align = "center"
    }
)
oMemoryLabel:setFillColor( 0 )
oGroup:insert(oMemoryLabel)

oTextureLabel = display.newText( 
    { 
        text = '',
        x = display.contentWidth*0.66,
        y = display.contentHeiht*0.66,
        font = 'Arial',
        fontSize = 32,
        align = "center"
    }
)
oTextureLabel:setFillColor( 0 )
oGroup:insert(oTextureLabel)

-- To check available memory
function checkMemory()
    collectgarbage( "collect" )
    oMemoryLabel.text = string.format( "M:%.2f KB", collectgarbage( "count" ) )
    oTextureLabel.text = "T:"..(string.format( "%.2f KB", system.getInfo("textureMemoryUsed") / (1024 * 1024)) )
end

timer.performWithDelay( 1000, checkMemory, 0 )

2 种使用方法,要么只加载一次,作为舞台图层在所有其他图层之上,这样它就不会在加载阶段之间被卸载,或者你现在只需在每个舞台上重新创建它。 ..它仍然会给你正确的号码。 如果场景之间的内存使用或纹理使用不断增加,坏消息,你可能有泄漏或某些东西没有被正确删除(因此最终会产生奇怪的崩溃)。 还是祝你好运:)

干杯

【讨论】:

  • 我测试了它,发现每个场景我的内存使用量增加了 1 或 2。我的内存使用量从 301.36kb 开始正常吗?
  • 我的每个代码文件的 createScene 都有一个 storyboard.removeAll(),根据该站点,删除场景将完全卸载模块。”所以我认为它也释放了内存?
  • 如果它在每个场景之间不断增加,那么你一定有泄漏...尝试做场景 1 -> 场景 2 -> 3 -> ... -> 10 -> 1 -> 2一段时间,看看它是否不断增加。如果是这样,那么您的代码中有问题。没有看到你的代码很难。一个建议是只尝试场景 1 -> 场景 2 -> 场景 1 -> 等,看看它是否也会增加:) 在这种情况下,你可以缩小范围
【解决方案2】:

移除场景不会停止场景中的物理,也不会停止任何运行时侦听器。计时器,转换,任何带有回调的东西仍然会运行,直到你停止它们。如果与这些事件相关联的对象不存在,您将遇到奇怪的崩溃。

确保正确清理场景。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    相关资源
    最近更新 更多