I am trying to restart the game but couldn’t. Showing a error message on returning to the previous function like.
Corona Simulator Runtime error
Attempt to call method ‘applyLinearImpulse’ (a nil value)
File: main.lua
Line: 136
[lua]
local physics = require(“physics”)
physics.start()
physics.setDrawMode( “normal” )
display.setStatusBar(display.HiddenStatusBar)
local _W = display.contentWidth
local _H = display.contentHeight
–create game screen
function gamePlayScreen()
local bg = display.newRect(0,0,_W, _H)
bg:setFillColor(0.9,0.6,0.2)
bg.x=centerX
bg.y=centerY
local lWall = display.newLine(0,0,0,_H)
lWall.strokeWidth = 5
local rWall = display.newLine(_W,0,_W,_H)
rWall.strokeWidth=5
local ceiling = display.newLine(0,0,_W,0)
ceiling.strokeWidth=5
local ground = display.newRect(0,_H, _W*2,30)
ground:setFillColor(0,0.5,0.2)
bodyProperty = {density=1.0, friction=0.3, bounce=0.4}
physics.addBody(lWall, “static”, bodyProperty)
physics.addBody(rWall, “static”, bodyProperty)
physics.addBody(ceiling, “static”, bodyProperty)
physics.addBody(ground, “static”, bodyProperty)
startGame()
--print(display.pixelWidth…" X "…display.pixelHeight)
end
–game functions
function startGame()
ball = display.newCircle(80,_H-55, 10)
ball:setFillColor(0.9,0,0)
physics.addBody(ball, “dynamic”, {density=0.1, friction=0.1, bounce=0.3})
ballStand = display.newRect(80, _H-40, 50,10 )
ballStand:setFillColor(0,0,0, 0.2)
physics.addBody(ballStand, “static”, bodyProperty)
–Creat Rotating on Pull
pull = display.newImage( “target.png” )
pull.x = ball.x; pull.y = ball.y; pull.alpha = 0;
function triggerBall(event)
if event.phase == “began” then
display.getCurrentStage():setFocus(ball)
ball.isFocus = true
– Stop current cueball motion, if any
pull.x = ball.x
pull.y = ball.y
startRotation = function()
pull.rotation = pull.rotation + 4
end
Runtime:addEventListener( “enterFrame”, startRotation )
local showPull = transition.to( pull, { alpha=0.4, xScale=0.25, yScale=0.25, time=200 } )
myLine = nil
elseif event.phase == "moved"then
if ( myLine ) then
myLine.parent:remove( myLine ) – erase previous line, if any
end
myLine = display.newLine( ball.x,ball.y, event.x,event.y ) – draw new line
myLine:setStrokeColor( 1, 1, 1, 0.5)
myLine.strokeWidth = 7
elseif event.phase == “ended” then
local stopRotation = function()
Runtime:removeEventListener( “enterFrame”, startRotation )
end
local hidePull = transition.to( pull, { alpha=0, xScale=0.5, yScale=0.5, time=200, onComplete=stopRotation } )
ball:applyLinearImpulse((event.xStart - event.x)/60, (event.yStart - event.y)/60, ball.x, ball.y)
display.getCurrentStage():setFocus(nil)
audio.play(ballShot)
if ( myLine ) then
myLine.parent:remove( myLine )
myLine = nil
end
end
return true
end
ball:addEventListener(“touch”, triggerBall )
level1()
end
–Level 1
function level1( )
local obs1 = display.newRect(_W*0.8, _H-60, 20, 90)
local obs2 = display.newRect(_W*0.8, _H-240, 20, _H-160)
local obs3 = display.newRect(_W*0.90, _H-140, 10, 80)
obs3.rotation = -39
local target = display.newRect(_W*0.90,_H-30, 40,5)
target:setFillColor(0.4,0.5,0.2)
physics.addBody(obs1, “static”, bodyProperty)
physics.addBody(obs2, “static”, bodyProperty)
physics.addBody(obs3, “static”, bodyProperty)
physics.addBody(target, “static”, bodyProperty)
local function onCollition(event)
if event.phase == “began” then
print(“You Won”)
audio.play(hitTarget)
display.remove(ball)
target:removeEventListener(“collision”, onCollition)
startGame()
end
return true
end
target:addEventListener(“collision”, onCollition)
end
startGame()
[/lua]