【发布时间】: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 时,碰撞侦听器突然不起作用,但它在一开始就起作用,是否有任何特殊原因会这样做?