I really need some help on this. I have a code that goes from scene1 to scene 5, from Question1 to 2,3,4 and then to 5. For some reason the corona simulator freezes at Question5 but on the debugger the print statements in Question 5 runs.(It did not give me errors, simulator just simply froze) I thought it might be the code problem but when Ichanged scene 5 to Question 1, the ballCollision function doesn’t work on scene 5 but it works at scene 1. I tried every ways i could think of, even taking memory leak precautions but it doesn’t work. I could not think of a reason why it works at scene 1 at the start and not the second time. Any ideas? My code Question1 is like the one below:
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 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
---------------------------------------------------------------------------------
storyboard.removeAll()
function scene:createScene( event )
local group = self.view
print(‘purge scene’)
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 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(“Question2”, “fade”, 2000)
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(“Question2”, “fade”, 2000)
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(“Question2”, “fade”, 2000)
end
end
end
end
end
function scene:enterScene(event)
local group = self.view
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
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)
–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