Hello,
I am new to Corona SDK. I am trying to restart a game using storyboard.gotoScene on another lua file then use storyboard.removeScene() then storyboard.gotoScene. I always received “attempt to call method ‘applyLinearImpulse’ (a nil value)” error. It watch the tutorial about this technique but it does not seem to work for me. Can someone tell me what’s wrong with my codes.
Thanks,
Quincy
require “CiderDebugger”;
–
– game.lua
–requires
local storyboard = require(“storyboard”)
local scene = storyboard.newScene()
local physics = require “physics”
physics.start()
local rocket
function propel(self, event)
rocket:play()
rocket:applyLinearImpulse(0, -10, rocket.x, rocket.y)
end
local function restartGame(event)
if (“ended” == event.phase) then
storyboard.gotoScene(“restart”, {time=250, effect=“crossFade”})
end
end
local function checkLocation(event)
if rocket.y < 50 then
--print (rocket.y)
storyboard.gotoScene(“complete”, {time=250, effect=“crossFade”})
end
end
function scene:createScene(event)
local screenGroup = self.view
local nightSky = display.newImage(“nightsky.png”, display.contentCenterX, display.contentCenterY)
nightSky:addEventListener(“touch”, propel)
screenGroup:insert(nightSky)
local ground = display.newImage(“moon.png”)
ground.x = display.contentCenterX
ground.y = 0
physics.addBody(ground, “static”)
screenGroup:insert(ground)
local top = display.newImage(“moon.png”)
top.x = display.contentCenterX
top.y = 1024
physics.addBody(top, “static”)
screenGroup:insert(top)
--rocket = display.newImage(“rocket.png”, display.contentCenterX, 1024-200)
local rocketOptions = {
width = 36,
height = 64,
numFrames = 3,
sheetContentWidth = 108,
sheetContentHeight = 64
}
local rocketSpriteSheet = graphics.newImageSheet(“rocket_launch.png”, rocketOptions)
local sequenceData = {
start = 1,
count = 3,
time = 1800,
loopCount =1,
loopDirection = “forward”
}
rocket = display.newSprite(rocketSpriteSheet , sequenceData)
rocket.x = display.contentCenterX
rocket.y = display.contentHeight/2
rocket:play()
physics.addBody(rocket, “dynamic”, {density= 1.05, bounce=0.1, friction=0.1, radius=23})
screenGroup:insert(rocket)
local btnRestart = display.newImage(“restart.png”, 48, 48)
btnRestart:addEventListener(“touch”, restartGame)
screenGroup:insert(btnRestart)
end
function scene:enterScene(event)
local screenGroup = self.view
rocket.enterFrame = checkLocation
Runtime:addEventListener(“enterFrame”, checkLocation)
end
function scene:exitScene(event)
Runtime:removeEventListener(“touch”, propel)
physics.stop()
end
function scene:destroyScene(event)
local screenGroup = self.view
end
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )
return scene
–
–
– restart.lua
–requires
local storyboard = require(“storyboard”)
local scene = storyboard.newScene()
function scene:createScene(event)
local screenGroup = self.view
--Background
local sky = display.newImage(“nightsky.png”)
sky.x = display.contentCenterX
sky.y = display.contentCenterY
screenGroup:insert(sky)
end
function scene:enterScene(event)
local screenGroup = self.view
storyboard.removeScene(“game”)
storyboard.gotoScene(“game”, {time=250, effect=“crossFade”})
end
function scene:exitScene(event)
local screenGroup = self.view
end
function scene:destroyScene(event)
local screenGroup = self.view
end
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )
return scene