When I run this game with storyboard I get this error after the tutorial level is complete. Below are my codes. I have my main pointing to start, then tutorial. I have my onCollision set to then move to level one. Thats where the problems start. Level one loads and scrolls and my missiles work but the rocket does not move and doesn’t collide with any missles. From there I can’t do anything. If i point my start to level 1 it does the same thing for lvl 2. Now if I run any of these codes by themselves starting from main they all work fine. Any idea of whats going wrong?
Error Message: 2013-01-14 14:11:34.981 Corona Simulator[4117:707] Runtime error
…ions/CoronaSDK/SampleCode/mkjt88/Scroller/level1.lua:102: attempt to call method ‘applyForce’ (a nil value)
stack traceback:
[C]: in function ‘applyForce’
…ions/CoronaSDK/SampleCode/mkjt88/Scroller/level1.lua:102: in function <…ions>
?: in function <?:229>
[lua]-----------------------------------------------------------------------------------------
–
– Main.lua
–
-----------------------------------------------------------------------------------------
display.setStatusBar(display.HiddenStatusBar)
local storyboard = require "storyboard"
storyboard.gotoScene(“start”)
-----------------------------------------------------------------------------------------
–
– start.lua
–
-----------------------------------------------------------------------------------------
screenW = display.contentWidth
screenH = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
local phyics = require “physics” ; physics.setDrawMode( “normal” )
physics.start()
function scene:createScene(event)
local screenGroup = self.view
background = display.newImage(“startScreen.png”)
screenGroup:insert(background)
end
function start(event)
if event.phase == “began” then
storyboard.gotoScene(“tutorial”)
end
end
function scene:enterScene(event)
background:addEventListener(“touch”, start)
end
function scene:exitScene(event)
background:removeEventListener(“touch”, start)
end
function scene:destroyScene(event)
end
scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)
return scene
-----------------------------------------------------------------------------------------
–
– tutorial.lua
–
-----------------------------------------------------------------------------------------
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
screenW = display.contentWidth
screenH = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)
local phyics = require “physics” ; physics.setDrawMode( “normal” )
physics.start()
– Boundaries
ceiling = display.newRect (0, screenH - 350, screenW, 0)
floor = display.newRect (0, screenH + 30, screenW, 0)
– Make Walls Physical
physics.addBody (ceiling, “static”, {friction = .2, bounce = .1})
physics.addBody (floor, “static”, {friction = .2, bounce = .1})
function scene:createScene(event)
local screenGroup = self.view
– Background Images
background1 = display.newImage(“background.png”)
background1:setReferencePoint(display.BottomLeftReferencePoint)
background1.x = 0
background1.y = 320
background1.speed = 8
screenGroup:insert(background1)
background2 = display.newImage(“background.png”)
background2:setReferencePoint(display.BottomLeftReferencePoint)
background2.x = 480
background2.y = 320
background2.speed = 8
screenGroup:insert(background2)
– Rocket
rocket = display.newImage(“rocket.png”)
rocket.x = 100
rocket.y = 160
physics.addBody(rocket, “dynamic”, {density = .1, friction = .5, radius = 25})
screenGroup:insert(rocket)
– Missiles
missile1 = display.newImage(“missile.png”)
missile1.x = 500
missile1.y = 100
missile1.speed = math.random(4,12)
missile1.initY = missile1.y
missile1.amp = math.random(1,100)
missile1.angle = math.random(1,360)
physics.addBody(missile1, “static”, {density = .1, bounce = .1, friction = .2})
screenGroup:insert(missile1)
missile2 = display.newImage(“missile.png”)
missile2.x = 500
missile2.y = 250
missile2.speed = math.random(7,15)
missile2.initY = missile2.y
missile2.amp = math.random(1,100)
missile2.angle = math.random(1,360)
physics.addBody(missile2, “static”, {density = .1, bounce = .1, friction = .2})
screenGroup:insert(missile2)
end
----------- Functions
function onTouch(event)
if event.phase == “began” then
rocket.enterFrame = activateRocket
Runtime:addEventListener(“enterFrame”, rocket)
end
if event.phase == “ended” then
Runtime:removeEventListener(“enterFrame”, rocket)
end
end
function scrollField(self,event)
if self.x < -475 then
self.x = 475
else
self.x = self.x - self.speed
end
end
function activateRocket(self,event)
self:applyForce(0, -15, self.x, self.y)
end
function moveMissile(self,event)
if self.x < -50 then
self.x = 500
self.y = math.random(20,300)
self.speed = math.random(2,5)
self.amp = math.random(20,50)
self.angle = math.random(1,200)
else
self.x = self.x - self.speed
self.angle = self.angle + .1
self.y = self.amp * math.sin(self.angle)+self.initY
end
end
function onCollision(event)
if event.phase == “began” then
storyboard.gotoScene(“level1”, “fade”, 400)
end
end
function scene:enterScene(event)
background1.enterFrame = scrollField
Runtime:addEventListener(“enterFrame”, background1)
background2.enterFrame = scrollField
Runtime:addEventListener(“enterFrame”, background2)
missile1.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile1)
missile2.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile2)
Runtime:addEventListener(“touch”, onTouch)
Runtime:addEventListener(“collision”, onCollision)
end
function scene:exitScene(event)
Runtime:removeEventListener(“enterFrame”, background1)
Runtime:removeEventListener(“enterFrame”, background2)
Runtime:removeEventListener(“enterFrame”, missile1)
Runtime:removeEventListener(“enterFrame”, missile2)
Runtime:removeEventListener(“touch”, onTouch)
Runtime:removeEventListener(“collision”, onCollision)
end
function scene:destroyScene(event)
end
scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)
return scene
-----------------------------------------------------------------------------------------
–
– level1.lua
–
-----------------------------------------------------------------------------------------
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
screenW = display.contentWidth
screenH = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)
local phyics = require “physics” ; physics.setDrawMode( “normal” )
physics.start()
– Boundaries
ceiling = display.newRect (0, screenH - 350, screenW, 0)
ceiling.name = "ceiling"
floor = display.newRect (0, screenH + 30, screenW, 0)
floor.name = “floor”
– Make Walls Physical
physics.addBody (ceiling, “static”, {friction = .2, bounce = .1})
physics.addBody (floor, “static”, {friction = .2, bounce = .1})
function scene:createScene(event)
local screenGroup = self.view
– Background Images
backgroundKep = display.newImage(“bgKep.png”, true)
backgroundKep:setReferencePoint(display.BottomLeftReferencePoint)
backgroundKep.x = 0
backgroundKep.y = 320
backgroundKep.speed = 5
screenGroup:insert(backgroundKep)
backgroundKep2 = display.newImage(“bgKep.png”, true)
backgroundKep2:setReferencePoint(display.BottomLeftReferencePoint)
backgroundKep2.x = 6077
backgroundKep2.y = 320
backgroundKep2.speed = 5
screenGroup:insert(backgroundKep2)
– Rocket
rocket = display.newImage(“rocket.png”)
rocket.x = 100
rocket.y = 160
rocket.name = “rocket”
physics.addBody(rocket, “dynamic”, {density = .1, friction = .5, radius = 20})
screenGroup:insert(rocket)
– Missiles
missile1 = display.newImage(“missile.png”)
missile1.x = 500
missile1.y = 100
missile1.speed = math.random(4,12)
missile1.initY = missile1.y
missile1.amp = math.random(1,100)
missile1.angle = math.random(1,360)
missile1.name = “missile1”
physics.addBody(missile1, “static”, {density = .1, bounce = .1, friction = .2})
screenGroup:insert(missile1)
missile2 = display.newImage(“missile.png”)
missile2.x = 500
missile2.y = 250
missile2.speed = math.random(7,15)
missile2.initY = missile2.y
missile2.amp = math.random(1,100)
missile2.angle = math.random(1,360)
missile2.name = “missile2”
physics.addBody(missile2, “static”, {density = .1, bounce = .1, friction = .2})
screenGroup:insert(missile2)
end
------ Functions
function onTouch(event)
if event.phase == “began” then
rocket.enterFrame = activateRocket
Runtime:addEventListener(“enterFrame”, rocket)
end
if event.phase == “ended” then
Runtime:removeEventListener(“enterFrame”, rocket)
end
end
local function scrollBack(self,event)
if self.x < -6077 then
self.x = 6077
else
self.x = self.x - self.speed
end
end
function activateRocket(self,event)
self:applyForce(0, -10, self.x, self.y)
end
local function moveMissile(self,event)
if self.x < -50 then
self.x = 500
self.y = math.random(20,300)
self.speed = math.random(2,5)
self.amp = math.random(20,50)
self.angle = math.random(1,200)
else
self.x = self.x - self.speed
self.angle = self.angle + .1
self.y = self.amp * math.sin(self.angle)+self.initY
end
end
function onCollision(event)
if event.phase == “began” then
storyboard.gotoScene(“level2”, “fade”, 400)
end
end
function scene:enterScene(event)
backgroundKep.enterFrame = scrollBack
Runtime:addEventListener(“enterFrame”, backgroundKep)
backgroundKep2.enterFrame = scrollBack
Runtime:addEventListener(“enterFrame”, backgroundKep2)
missile1.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile1)
missile2.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile2)
Runtime:addEventListener(“touch”, onTouch)
Runtime:addEventListener(“collision”, onCollision)
end
function scene:exitScene(event)
Runtime:removeEventListener(“enterFrame”, backgroundKep)
Runtime:removeEventListener(“enterFrame”, backgroundKep2)
Runtime:removeEventListener(“enterFrame”, missile1)
Runtime:removeEventListener(“enterFrame”, missile2)
Runtime:removeEventListener(“touch”, onTouch)
Runtime:removeEventListener(“collision”, onCollision)
end
function scene:destroyScene(event)
end
scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)
return scene
-----------------------------------------------------------------------------------------
–
– level2.lua
–
-----------------------------------------------------------------------------------------
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
screenW = display.contentWidth
screenH = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)
local phyics = require “physics” ; physics.setDrawMode( “normal” )
physics.start()
– Boundaries
ceiling = display.newRect (0, screenH - 350, screenW, 0)
ceiling.name = "ceiling"
floor = display.newRect (0, screenH + 30, screenW, 0)
floor.name = “floor”
– Make Walls Physical
physics.addBody (ceiling, “static”, {friction = .2, bounce = .1})
physics.addBody (floor, “static”, {friction = .2, bounce = .1})
function scene:createScene(event)
local screenGroup = self.view
– Background Images
backgroundInc = display.newImage(“bgInc.png”, true)
backgroundInc:setReferencePoint(display.BottomLeftReferencePoint)
backgroundInc.x = 0
backgroundInc.y = 320
backgroundInc.speed = 5
screenGroup:insert(backgroundInc)
backgroundInc2 = display.newImage(“bgInc.png”, true)
backgroundInc2:setReferencePoint(display.BottomLeftReferencePoint)
backgroundInc2.x = 8190
backgroundInc2.y = 320
backgroundInc2.speed = 5
screenGroup:insert(backgroundInc2)
– Rocket
rocket = display.newImage(“rocket.png”)
rocket.x = 100
rocket.y = 160
rocket.name = “rocket”
physics.addBody(rocket, “dynamic”, {density = .1, friction = .5, radius = 20})
screenGroup:insert(rocket)
– Missiles
missile1 = display.newImage(“missile.png”)
missile1.x = 500
missile1.y = 100
missile1.speed = math.random(4,12)
missile1.initY = missile1.y
missile1.amp = math.random(1,100)
missile1.angle = math.random(1,360)
missile1.name = “missile1”
physics.addBody(missile1, “static”, {density = .1, bounce = .1, friction = .2})
screenGroup:insert(missile1)
missile2 = display.newImage(“missile.png”)
missile2.x = 500
missile2.y = 250
missile2.speed = math.random(7,15)
missile2.initY = missile2.y
missile2.amp = math.random(1,100)
missile2.angle = math.random(1,360)
missile2.name = “missile2”
physics.addBody(missile2, “static”, {density = .1, bounce = .1, friction = .2})
screenGroup:insert(missile2)
end
----- Functions
function onTouch(event)
if event.phase == “began” then
rocket.enterFrame = activateRocket
Runtime:addEventListener(“enterFrame”, rocket)
end
if event.phase == “ended” then
Runtime:removeEventListener(“enterFrame”, rocket)
end
end
local function scrollBack(self,event)
if self.x < -8190 then
self.x = 8190
else
self.x = self.x - self.speed
end
end
function activateRocket(self,event)
self:applyForce(0, -10, self.x, self.y)
end
local function moveMissile(self,event)
if self.x < -50 then
self.x = 500
self.y = math.random(20,300)
self.speed = math.random(2,5)
self.amp = math.random(20,50)
self.angle = math.random(1,200)
else
self.x = self.x - self.speed
self.angle = self.angle + .1
self.y = self.amp * math.sin(self.angle)+self.initY
end
end
function onCollision(event)
if event.phase == “began” then
storyboard.gotoScene(“restart”, “fade”, 400)
end
end
function scene:enterScene(event)
backgroundInc.enterFrame = scrollBack
Runtime:addEventListener(“enterFrame”, backgroundInc)
backgroundInc2.enterFrame = scrollBack
Runtime:addEventListener(“enterFrame”, backgroundInc2)
missile1.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile1)
missile2.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile2)
Runtime:addEventListener(“touch”, onTouch)
Runtime:addEventListener(“collision”, onCollision)
end
function scene:exitScene(event)
Runtime:removeEventListener(“enterFrame”, backgroundInc)
Runtime:removeEventListener(“enterFrame”, backgroundInc2)
Runtime:removeEventListener(“enterFrame”, missile1)
Runtime:removeEventListener(“enterFrame”, missile2)
Runtime:removeEventListener(“touch”, onTouch)
Runtime:removeEventListener(“collision”, onCollision)
end
function scene:destroyScene(event)
end
scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)
return scene
-----------------------------------------------------------------------------------------
–
– Restart.lua
–
-----------------------------------------------------------------------------------------
screenW = display.contentWidth
screenH = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
local phyics = require “physics” ; physics.setDrawMode( “normal” )
physics.start()
function scene:createScene(event)
local screenGroup = self.view
background = display.newImage(“restart.png”)
screenGroup:insert(background)
end
function start(event)
if event.phase == “began” then
storyboard.gotoScene(“main”)
end
end
function scene:enterScene(event)
background:addEventListener(“touch”, start)
end
function scene:exitScene(event)
background:removeEventListener(“touch”, start)
end
function scene:destroyScene(event)
end
scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)
return scene [import]uid: 20272 topic_id: 34942 reply_id: 334942[/import] </…ions>