this error irritates me

hey

why i am stuck with this error?? its kinda irritating even though my code seems to be okay

ERROR: C:\Users\admin\Documents\Corona Projects\Game_2\freePlay\freePlay.lua:644: physics.start() must be called before physics.addBody()

15:42:56.382  ERROR: C:\Users\admin\Documents\Corona Projects\Game_2\freePlay\freePlay.lua:741: physics.start() must be called before physics.addBody()

15:42:56.382  ERROR: C:\Users\admin\Documents\Corona Projects\Game_2\freePlay\freePlay.lua:750: physics.start() must be called before physics.addBody()

15:42:56.382  ERROR: C:\Users\admin\Documents\Corona Projects\Game_2\freePlay\freePlay.lua:756: physics.start() must be called before physics.addBody()

15:42:56.382  ERROR: C:\Users\admin\Documents\Corona Projects\Game_2\freePlay\freePlay.lua:762: physics.start() must be called before physics.addBody()

i have two games in my one game…that is, in my main game, i have two sub games…when i open one game, it works fine… then i go to another game, then also it works fine…if i continue this process of going from one game to other, it works… but sometimes it gives me an error when going to either the first game or second game…

in menu.lua i remove this game scenes…though i am getting this error

this is first game’s lua code:

Code which takes me to menu scene is highlighted in red in this code

local composer = require( "composer" ) local widget = require "widget" local scene = composer.newScene() local physics = require "physics" physics.start() physics.setGravity(0,13) physics.setDrawMode("normal") -- sometimes physics.addbody cannot called befpre physics start user = loadsave.loadTable("user.json") -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- -- time local timeCounter = 0 local enemyOver = 0 -- enemies local virus = {} -- enemies table local enemyTravelSpeed = 1700 -- enemy travel speed across the screen local enemySendSpeed = 100 local enemyCounter = 0 local enemyIncrementSpeed = 3 local enemyMaxSendSpeed = 7 local trans\_sendEnemies local bombs = {} local bombCounter = 0 local poofSheet local poofImage -- score properties --user.score = 0 local scoreText local score = 0 -- other properties local ball, ground, leftWall, rightWall, whiteBg, blackBg, notEnoughMoney, upWall local ballRadius = 40 -- if isScoringUp is set to true, then, when enemy collides with the ground, score up, else don't score up. local isScoringUp = true local onGameOver -- once again pop up local onceAgainBg, btn\_onceAgainPlay, btn\_onceAgainQuit, onceAgainText, rubyBar,ruby, rubyText local countdownImage -- current ruby level local currentLevel = 1 -- function local sendEnemies local scoreUp local onCollision local dragMe local onPauseTouch -- tmrs and trans local trans\_remove\_virus, trans\_remove\_ball, enemy\_shock, ball\_shock , trans\_showWhiteBg local trans\_remove\_ball, trans\_remove\_virus, trans\_onceAgainTitle, trans\_onceAgainTitle2 -- pause local resumeBtn, pauseBtn, homeButton, btn\_soundOn, btn\_soundOff local pauseIsActive = true -- resistance local resistance local isResistanceEnabled = false local timer\_res, trans\_res, trans\_res2 -- keys local keys = {} , keyRand local keyCounter = 0 -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- composer.removeScene("freePlay.gameOver") --composer.loadScene("scene\_menu", true ) -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on -- on pause visuals touch --[[local function onSoundTouch( event ) -- body if (event.phase == "ended") then if user.playSound == true then -- mute the sound audio.setVolume(0) btn\_soundOn.alpha = 0 btn\_soundOff.alpha = 1 user.playSound = false loadsave.saveTable(user, "user.json") elseif user.playSound == false then -- unmute the sound audio.setVolume(1) btn\_soundOn.alpha = 1 btn\_soundOff.alpha = 0 user.playSound = true loadsave.saveTable(user, "user.json") end end return true end--]] local function onHomeTouch( event ) -- body if event.phase == "ended" then Runtime:removeEventListener("enterFrame",sendEnemies) Runtime:removeEventListener("collision", onCollision) transition.cancel() physics.stop() if trans\_sendEnemies then transition.cancel(trans\_sendEnemies) trans\_sendEnemies = nil end if trans\_key then transition.cancel(trans\_key) trans\_key = nil end if trans\_res and trans\_res2 then transition.cancel(trans\_res) trans\_res = nil transition.cancel(trans\_res2) trans\_res2 = nil end composer.gotoScene("scene\_menu","crossFade",500) end return true end local function onResumeTouch( event ) -- body if event.phase == "ended" then if timer\_res then timer.resume(timer\_res) end -- pause button's enability pauseBtn:setEnabled(true) pauseBtn.alpha = 1 -- resume the timer -- resume the physics engine and transition physics.start() transition.resume() -- runtime event listeners --Runtime:addEventListener("enterFrame",sendEnemies) Runtime:addEventListener("collision", onCollision) Runtime:addEventListener("enterFrame",sendEnemies) if user.resistanceLevel == 1 then if resistance.isVisible == true then resistance:setEnabled(true) end end -- pause visuals blackBg.alpha = 0 resumeBtn.alpha = 0 homeButton.alpha = 0 btn\_soundOn.alpha = 0 btn\_soundOff.alpha = 0 end return true end function onPauseTouch( event ) -- body -- pause the timer start again, physics, transition, countdownimage, -- add event listeners -- set pause visuals' alpha to 0 -- decrease the alpha of pause button and set its enability to false if event.phase == "ended" then if pauseIsActive == true then print("pause button clicked") -- decrease the alpha of pause button and set its enability to false -- set the enemies button's alpha to 0.5 pauseBtn:setEnabled(false) pauseBtn.alpha = 0.5 -- pause the transition and physics transition.pause() physics.pause() --if resistance shield is transitioning, pause it if timer\_res then timer.pause(timer\_res) end -- remove runtime event listeners Runtime:removeEventListener("enterFrame",sendEnemies) Runtime:removeEventListener("collision", onCollision) -- show pause visuals blackBg.alpha = 0.7 resumeBtn.alpha = 1 homeButton.alpha = 1 -- set resistance enability to false if user.resistanceLevel == 1 then if resistance.isVisible == true then resistance:setEnabled(false) end end if (user.playSound == true ) then print("audio") btn\_soundOn.alpha = 1 elseif (user.playSound == false ) then print("no audio") btn\_soundOff.alpha = 1 end end end return true end -- drag me function for ball function dragMe(event) --print("ball touched") local self=event.target if event.phase == "began" then display.getCurrentStage():setFocus(self,event.id) --create a touch joint so that gravity doesn't pull the object down self.tempJoint = physics.newJoint( "touch", self, event.x, event.y ) self.isFocus = true self.isFixedRotation = true --stop the object from rotating while being dragged elseif self.isFocus then if event.phase == "moved" then self.tempJoint:setTarget( event.x, event.y ) elseif event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus(self,nil) self.isFixedRotation = false self.isFocus = false self.tempJoint:removeSelf() end end return true end -- sending enemies function sendEnemies() -- body -- timeCounter : keeps track of the time in the game, starts at 0 -- enemySendSpeed : will tell us how often to send the enemies, starts at 100 -- enemyCounter : keeps track of the number of enemies on the screen, starts at 0 -- enemyIncrementSpeed : how much to increase the enemy speed, starts at 10 -- enemyMaxSendSpeed : limit the send speed to 7 --print("sending enemies") timeCounter = timeCounter + 1 if ((timeCounter%enemySendSpeed == 0)) then enemyCounter = enemyCounter + 1 enemySendSpeed = enemySendSpeed - enemyIncrementSpeed if enemySendSpeed \>= 50 then enemyIncrementSpeed = 3 end if enemySendSpeed \<= 50 then enemyIncrementSpeed = 1 end if (enemySendSpeed \<= enemyMaxSendSpeed) then enemySendSpeed = enemyMaxSendSpeed enemyTravelSpeed = 1500 end if score \> 30000 then enemyTravelSpeed = 1200 enemyMaxSendSpeed = 5 end if score \> 100000 then enemyTravelSpeed = 1000 enemyMaxSendSpeed = 5 end if score \> 150000 then enemyTravelSpeed = 900 enemyMaxSendSpeed = 5 end print(enemySendSpeed) print(enemyTravelSpeed) -- virus[enemyCounter] = display.newImageRect(sceneGroup ,"freePlay/images/virus.png",100,100) virus[enemyCounter] = display.newRoundedRect(sceneGroup , 0,0, 18,70,10) virus[enemyCounter].x = math.random(screenLeft + virus[enemyCounter].width , screenRight - virus[enemyCounter].width) virus[enemyCounter].y = screenTop - virus[enemyCounter].height virus[enemyCounter].name = "enemy" virus[enemyCounter]:setFillColor(1, 0.92, 0.56) physics.addBody(virus[enemyCounter],"dynamic") virus[enemyCounter].isFixedRotation = true blackBg:toFront() resumeBtn:toFront() btn\_soundOn:toFront() btn\_soundOff:toFront() homeButton:toFront() trans\_sendEnemies = transition.to(virus[enemyCounter], {y = screenBottom - virus[enemyCounter].height \* 0.5, time =enemyTravelSpeed,onComplete = function () enemyOver = enemyOver +1 end }) end end --scoring up function scoreUp( event ) -- body score = score + 100 user.score = score loadsave.saveTable(user,"user.json") scoreText.text = "Score:"..score scoreText.anchorX =1 scoreText.x = screenRight - scoreText.width \* 0.2 end ------------------------------------------------ ----------------------------------------------- -----------game over------------------------------- --------------------------------------------------- function gameOver(event ) -- body -- remove event listeners Runtime:removeEventListener("enterFrame",sendEnemies) Runtime:removeEventListener("collision",onCollision) ball:removeEventListener("touch",dragMe) -- puase is active pauseIsActive = false -- show white bg instantly whiteBg.alpha = 0.5 trans\_showWhiteBg = transition.to(whiteBg,{alpha = 0 , time =10}) -- remove ball trans\_remove\_ball = transition.to(ball, {alpha = 0, time = 500}) if score == 0 then user.score = 0 loadsave.saveTable(user,"user.json") end -- remove transitions transition.cancel(trans\_sendEnemies) trans\_sendEnemies = nil -- send enemies off screen for i = 1, #virus do print("rotation") trans\_enemies = transition.to( virus[i], {x = screenLeft - 100,y = centerY, transition = easing.inOutElastic, time=2000 , onComplete = function (self)display.remove(self); self = nil;transition.cancel(trans\_enemies); trans\_enemies = nil;physics.stop(); composer.gotoScene("freePlay.gameOver","fade",500) end } ) end end -- onCollision function onCollision(event) if (event.phase == "began") then if (event.object1.name == "ball" and event.object2.name == "enemy") then if isResistanceEnabled == false then if event.object2 ~= nil then --display.remove(event.object2) print("ohoooo") timer.performWithDelay(20,gameOver(event.object1, event.object2),1) --onGameOver(event.object1, event.object2) end elseif isResistanceEnabled == true then if event.object2 ~= nil then display.remove(event.object2) timer.performWithDelay(10,scoreUp,1) end end elseif (event.object2.name == "ball" and event.object1.name == "enemy")then if isResistanceEnabled == false then if event.object1 ~= nil then --display.remove(event.object1) print("ohoooo") timer.performWithDelay(20,gameOver(event.object2, event.object1),1) end elseif isResistanceEnabled == true then if event.object1 ~= nil then display.remove(event.object1) timer.performWithDelay(10,scoreUp,1) end end elseif (event.object1.name == "enemy" and event.object2.name == "ground")then if isScoringUp == true then if event.object1 ~= nil then print("Score Up") display.remove(event.object1) print(enemyOver) timer.performWithDelay(10,scoreUp,1) end elseif isScoringUp == false then if event.object1 ~= nil then print("No score Up") display.remove(event.object1) --scoreUp() end end elseif (event.object1.name == "ground" and event.object2.name == "enemy")then if isScoringUp == true then if event.object2 ~= nil then print(enemyOver) display.remove(event.object2) timer.performWithDelay(10,scoreUp,1) end elseif isScoringUp == false then if event.object2 ~= nil then print("No score Up") display.remove(event.object2) --scoreUp() end end elseif event.object1.name == "key" and event.object2.name == "ball" then print("keyyyyy") if event.object1 ~= nil then display.remove(event.object1) event.object1 = nil user.keys = user.keys + 1 rubyText.text = user.keys loadsave.saveTable(user,"user.json") end elseif event.object2.name == "key" and event.object1.name == "ball" then print("keyyyyy") if event.object2 ~= nil then display.remove(event.object2) event.object2 = nil user.keys = user.keys + 1 rubyText.text = user.keys loadsave.saveTable(user,"user.json") end elseif event.object1.name == "key" and event.object2.name == "ground" then print("no keyyyyy") if event.object1 ~= nil then display.remove(event.object1) event.object1 = nil end elseif event.object2.name == "key" and event.object1.name == "ground" then print("no keyyyyy") if event.object2 ~= nil then display.remove(event.object2) event.object2 = nil end end end end local function onResTouch( event ) -- body if (event.phase == "ended") then local resistanceShield = display.newRoundedRect(sceneGroup,scoreText.x - scoreText.width \*0.5,scoreText.y + scoreText.height \* 1.3,100, 10,50) resistance.isVisible = false user.resistanceLevel = user.resistanceLevel - 1 loadsave.saveTable(user,"user.json") if trans\_res and trans\_res2 then transition.cancel(trans\_res2) transition.cancel(trans\_res) trans\_res = nil trans\_res2 = nil end print(scoreText.width) local function reduceRes () resistanceShield.width = resistanceShield.width - 2 print(resistanceShield.width ) if resistanceShield.width == 0 then print("canceled resistance") isResistanceEnabled = false resistanceShield.isVisible = false timer.cancel(timer\_res) timer\_res = nil end end timer\_res = timer.performWithDelay(200,reduceRes,0) isResistanceEnabled = true end end local backgroundImage = display.newImageRect(sceneGroup,"freePlay/images/Background image.png",800,1400) backgroundImage.x = centerX backgroundImage.y = centerY backgroundImage.width = screenWidth backgroundImage.height = screenHeight scoreText = display.newText(sceneGroup, "Score:0",0,0,\_font,40) scoreText.anchorX = 1 scoreText.x = screenRight - scoreText.width \* 0.2 scoreText.y = screenTop + scoreText.height upWall = display.newRect(sceneGroup , 0,0,screenWidth, 10) upWall.x = centerX upWall.y = screenTop - upWall.height physics.addBody(upWall,"static", {filter = {category = 1, maskBits = 1 }}) whiteBg = display.newRect (sceneGroup , centerX, centerY, screenWidth, screenHeight) whiteBg.alpha = 0 notEnoughMoney = display.newText(sceneGroup, "Not Enough Rubies",0,0,\_font,50) notEnoughMoney.x = centerX notEnoughMoney.y = centerY + screenWidth \* 0.5 notEnoughMoney.alpha = 0 pauseBtn = widget.newButton { width = 100, height = 100, defaultFile = "freePlay/images/pause/pauseBtn.png", overFile = "freePlay/images/pause/pauseBtn\_over.png", onEvent = onPauseTouch } pauseBtn.x = screenLeft + pauseBtn.width pauseBtn.y = screenTop + pauseBtn.height \* 1.2 blackBg = display.newRect(centerX , centerY , screenWidth, screenHeight) blackBg.alpha = 0 blackBg:setFillColor(0) resumeBtn = widget.newButton { width = 250, height = 250 , defaultFile = "btn\_play.png", overFile = "btn\_play\_over.png", onEvent = onResumeTouch } resumeBtn.x = centerX resumeBtn.y = centerY - resumeBtn.height \* 0.5 resumeBtn.alpha = 0 homeButton = widget.newButton { width = 150, height = 150, defaultFile = "freePlay/images/pause/btn\_home.png", overFile = "freePlay/images/gameOver/btn\_home\_over.png", onEvent = onHomeTouch } homeButton.x = centerX homeButton.y = centerY + homeButton.height homeButton.alpha = 0 btn\_soundOn = widget.newButton { width = 70, height = 70, defaultFile = "freePlay/images/pause/soundOn.png", overFile = "freePlay/images/pause/soundOn\_over.png", onEvent = onSoundTouch } btn\_soundOn.x = centerX btn\_soundOn.y = centerY - resumeBtn.height \* 1.5 btn\_soundOn.alpha = 0 btn\_soundOff = widget.newButton { width = 70, height = 70, defaultFile = "freePlay/images/pause/soundOff.png", overFile = "freePlay/images/pause/soundOff\_over.png", onEvent = onSoundTouch } btn\_soundOff.x = btn\_soundOn.x btn\_soundOff.y = btn\_soundOn.y btn\_soundOff.alpha = 0 if user.isRedEnabled == true then ball = display.newImageRect(sceneGroup,"upImages/ball/red-white-ball.png",75,75) elseif user.isYellowEnabled == true then ball = display.newImageRect(sceneGroup,"upImages/ball/yellow-white-ball.png",75,75) elseif user.isPurpleEnabled == true then ball = display.newImageRect(sceneGroup,"upImages/ball/purple-white-ball.png",75,75) elseif user.isOrangeEnabled == true then ball = display.newImageRect(sceneGroup,"upImages/ball/orange-white-ball.png",75,75) elseif user.isBlueEnabled == true then ball = display.newImageRect(sceneGroup,"upImages/ball/blue-white-ball.png",75,75) elseif user.isGreenEnabled == true then ball = display.newImageRect(sceneGroup,"upImages/ball/green-white-ball.png",75,75) end ball.x = centerX ball.y = centerY ball.name = "ball" physics.addBody(ball,"dynamic",{radius = ballRadius, bounce = 0.4,filter = {category = 1, maskBits = 1 }}) ball:addEventListener("touch",dragMe) ground = display.newRect (sceneGroup,0,0,screenWidth,20) ground.x = centerX ground.y = screenBottom + ground.height \* 0.5 ground:setFillColor(1,1,1) ground.name = "ground" physics.addBody(ground,"static") leftWall = display.newRect (sceneGroup,0,0,20,screenHeight) leftWall.x = screenLeft - leftWall.width \* 0.5 leftWall.y = centerY leftWall:setFillColor(1,1,1) physics.addBody(leftWall,"static") rightWall = display.newRect (sceneGroup,0,0,20,screenHeight) rightWall.x = screenRight + rightWall.width \* 0.5 rightWall.y = centerY rightWall:setFillColor(1,1,1) physics.addBody(rightWall,"static") if user.resistanceLevel == 1 then resistance = widget.newButton { width = 70, height = 70, defaultFile = "freePlay/images/resistance.png", overFile = "freePlay/images/resistance\_over.png", onEvent = onResTouch } resistance.x = screenRight - resistance.width \* 1.3 resistance.y = scoreText.y + scoreText.height \* 1.3 local function transitionRes() trans\_res = transition.to(resistance,{time = 300, xScale = 0.8, yScale = 0.8}) trans\_res2 = transition.to(resistance,{delay= 300, time = 300, xScale = 1, yScale = 1, onComplete = transitionRes}) end transitionRes() sceneGroup:insert(resistance) end Runtime:addEventListener("enterFrame" ,sendEnemies) Runtime:addEventListener("collision", onCollision) sceneGroup:insert(blackBg) sceneGroup:insert(pauseBtn) sceneGroup:insert(resumeBtn) sceneGroup:insert(homeButton) sceneGroup:insert(btn\_soundOn) sceneGroup:insert(btn\_soundOff) -------------- --------- end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen local prevScene = composer.getSceneName("previous") if prevScene then print("Previous scene removed") composer.removeScene(prevScene) end end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) --ball:removeEventListener("touch",dragMe) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

You can see my gameOver(highlighted with green colour) function…whether i went wrong in something

this is my second game’s lua

gameover function is highlighted in green and compose.gotoscene is highlighted in red

local composer = require( "composer" ) local scene = composer.newScene() -- require physics library local physics = require "physics" physics.start() physics.setGravity(0,9.8) physics.setDrawMode("hybrid") -- widget library local widget = require "widget" -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- -- bg and walls local background, blackBg, whiteBg local leftWall, rightWall, midWall,ground,upWall local rightArrow, leftArrow local isArrowTouchEnable = true -- scoring module local score = 0 local scoreText local isScoringUp = true -- ball local ball local ballRadius = 40 -- enemies local sendEnemies local enemy = {} -- enemies table local enemyTravelSpeed = 1700 -- enemy travel speed across the screen local enemySendSpeed = 100 local enemyCounter = 0 local enemyIncrementSpeed = 3 local enemyMaxSendSpeed = 10 local timeCounter = 0 local trans\_enemy local tmr\_goto -- functions local onCollision local gameOver local onArrowTouch local onArrowTouch2 local scoreUp -- other property local isResistanceEnabled = false -- keys local keys = {} local keyCounter = 0 local trans\_key -- pause visuals -- pause local resumeBtn, pauseBtn, homeButton, btn\_soundOn, btn\_soundOff local pauseIsActive = true local onPauseTouch, onResumeTouch,onSoundTouch -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen -- background local function onHomeTouch( event ) -- body if event.phase == "ended" then Runtime:removeEventListener("enterFrame",sendEnemies) Runtime:removeEventListener("collision", onCollision) transition.cancel() physics.stop() if trans\_enemy then transition.cancel(trans\_enemy) trans\_enemy = nil end if trans\_key then transition.cancel(trans\_key) trans\_key = nil end -- user.score2 = 0 loadsave.saveTable(user,"user.json") if trans\_res and trans\_res2 then transition.cancel(trans\_res) trans\_res = nil transition.cancel(trans\_res2) trans\_res2 = nil end composer.gotoScene("scene\_menu","crossFade",500) end return true end function onResumeTouch( event ) -- body if event.phase == "ended" then -- add event listeners Runtime:addEventListener("collision",onCollision) Runtime:addEventListener("enterFrame",sendEnemies) leftArrow:addEventListener("touch",onArrowTouch) rightArrow:addEventListener("touch",onArrowTouch2) -- resuming physics and transitions. transition.resume() physics.start() -- resume the resistance timer if it is present if timer\_res then timer.pause(timer\_res) end -- set the pause buttons enability to false pauseBtn:setEnabled(true) pauseBtn.alpha = 1 -- show pause visuals blackBg.alpha = 0 resumeBtn.isVisible = false homeButton.isVisible = false if (user.playSound == true ) then print("audio") btn\_soundOn.isVisible = false elseif (user.playSound == false ) then print("no audio") btn\_soundOff.isVisible = false end end end function onPauseTouch( event ) -- body if event.phase == "ended" then if pauseIsActive == true then -- remove event listeners Runtime:removeEventListener("collision",onCollision) Runtime:removeEventListener("enterFrame", sendEnemies) leftArrow:removeEventListener("touch",onArrowTouch) rightArrow:removeEventListener("touch",onArrowTouch2) -- pausing the transitions and physics transition.pause() physics.pause() -- pause the resistance timer if it is present if timer\_res then timer.pause(timer\_res) end -- set the pause buttons enability to false pauseBtn:setEnabled(false) pauseBtn.alpha = 0.5 -- show pause visuals blackBg.alpha = 0.7 resumeBtn.isVisible = true homeButton.isVisible = true if (user.playSound == true ) then print("audio") btn\_soundOn.isVisible = true elseif (user.playSound == false ) then print("no audio") btn\_soundOff.isVisible = true end rightArrow:toFront() leftArrow:toFront() blackBg:toFront() resumeBtn:toFront() btn\_soundOn:toFront() btn\_soundOff:toFront() homeButton:toFront() end end return true end function sendEnemies() -- body -- timeCounter : keeps track of the time in the game, starts at 0 -- enemySendSpeed : will tell us how often to send the enemies, starts at 100 -- enemyCounter : keeps track of the number of enemies on the screen, starts at 0 -- enemyIncrementSpeed : how much to increase the enemy speed, starts at 10 -- enemyMaxSendSpeed : limit the send speed to 7 --print("sending enemies") timeCounter = timeCounter + 1 if ((timeCounter%enemySendSpeed == 0)) then enemyCounter = enemyCounter + 1 enemySendSpeed = enemySendSpeed - enemyIncrementSpeed if enemySendSpeed \>= 50 then enemyIncrementSpeed = 3 end if enemySendSpeed \<= 50 then enemyIncrementSpeed = 1 end if (enemySendSpeed \<= enemyMaxSendSpeed) then enemySendSpeed = enemyMaxSendSpeed enemyTravelSpeed = 1500 end if score \> 30000 then enemyTravelSpeed = 1200 enemyMaxSendSpeed = 5 end if score \> 100000 then enemyTravelSpeed = 1000 enemyMaxSendSpeed = 5 end if score \> 150000 then enemyTravelSpeed = 900 enemyMaxSendSpeed = 5 end print(enemySendSpeed) print(enemyTravelSpeed) -- virus[enemyCounter] = display.newImageRect(sceneGroup ,"freePlay/images/virus.png",100,100) enemy[enemyCounter] = display.newRoundedRect(sceneGroup , 0,0, 80,80,20) enemy[enemyCounter].x = math.random(screenLeft + enemy[enemyCounter].width , screenRight - enemy[enemyCounter].width) enemy[enemyCounter].y = screenTop - enemy[enemyCounter].height enemy[enemyCounter].name = "enemy" enemy[enemyCounter]:setFillColor(1, 0.92, 0.56) physics.addBody(enemy[enemyCounter],"dynamic") enemy[enemyCounter].isFixedRotation = true rightArrow:toFront() leftArrow:toFront() blackBg:toFront() resumeBtn:toFront() btn\_soundOn:toFront() btn\_soundOff:toFront() homeButton:toFront() trans\_enemy = transition.to(enemy[enemyCounter], {y = screenBottom - enemy[enemyCounter].height \* 0.5, time =enemyTravelSpeed }) end end function gameOver(obj1,obj2) -- obj1 = ball, obj2 = enemy -- disable timers, nil them -- remove event listeners -- set pauseactive to false pauseIsActive = false isArrowTouchEnable = false display.remove(obj1) -- remove arrows timer if timer\_right then timer.cancel(timer\_right) timer\_right = nil end if timer\_left then timer.cancel(timer\_left) timer\_left = nil end -- remove arrows event listener rightArrow:removeEventListener("touch",onArrowTouch) leftArrow:removeEventListener("touch",onArrowTouch) -- show white bg whiteBg.alpha = 0.5 trans\_showWhiteBg = transition.to(whiteBg,{alpha = 0 , time = 10,onComplete = function() trans\_showWhiteBg = nil end }) -- remove send enemies listener and collision Runtime:removeEventListener("enterFrame",sendEnemies) Runtime:removeEventListener("collision",onCollision) -- pause the physics physics.pause() transition.pause(trans\_enemy) -- remove keys if any for i = 1, #keys do if keys[i] ~= nil then display.remove(keys[i]) keys[i] = nil end end if trans\_key then transition.cancel(trans\_key) trans\_key = nil end -- transition enemies outside the screen for i =1 , #enemy do trans\_gameOver = transition.to( enemy[i], {x = screenLeft - obj2.width \* 2 ,y = 200, transition=easing.inBounce , time = 2000,onComplete = function(self) display.remove(self) self = nil tmr\_goto = timer.performWithDelay(500,function()tmr\_goto = nil ;composer.gotoScene("control.gameOver","fade",500)end,1) end } ) end transition.cancel(trans\_enemy) trans\_enemy = nil -- check wether score is zero if score2 == 0 then user.score2 = 0 loadsave.saveTable(user,"user.json") end loadsave.saveTable(user,"user.json") end --add pause, -- remove event listeners, and all that stuff function scoreUp() -- body score = score + 100 user.score2 = score scoreText.text = "Score:"..score scoreText.anchorX = 1 scoreText.x = screenRight - scoreText.width \* 0.2 loadsave.saveTable(user,"user.json") if user.score2 \>= 00 then local keyRand = math.random(1,200) if keyRand \> 0 then print("key rand is ';".. keyRand) keyCounter = keyCounter + 1 keys[keyCounter] = display.newImageRect( sceneGroup, "menuImages/ruby.png",40,60) keys[keyCounter].x = math.random(screenLeft + 80 , screenRight - 80) keys[keyCounter].y = screenTop - keys[keyCounter].height keys[keyCounter].name = "key" physics.addBody(keys[keyCounter],"dynamic") keys[keyCounter].isFixedRotation = true trans\_key = transition.to (keys[keyCounter],{y = screenBottom + keys[keyCounter].height , time = math.random(1200,1500)}) end end end function onCollision( event ) -- body print("collision listener running") if (event.phase == "began") then local obj1 = event.object1 local obj2 = event.object2 -- ball and enemy collision if (obj1.name == "ball" and obj2.name == "enemy") then -- check whether player has enabled resistance if isResistanceEnabled == true then -- resistance is enabled, kill the enemy and score up! elseif isResistanceEnabled == false then -- whoo! You're out timer.performWithDelay(10,gameOver(obj1, obj2),1) end elseif (obj2.name == "ball" and obj1.name == "enemy") then -- check whether player has enabled resistance if isResistanceEnabled == true then -- resistance is enabled, kill the enemy and score up! elseif isResistanceEnabled == false then -- whoo! You're out timer.performWithDelay(10,gameOver(obj2, obj1),1) end -- enemy and ground collision elseif (obj1.name == "ground" and obj2.name == "enemy") then if (isScoringUp == true) then display.remove(obj2) obj2 = nil timer.performWithDelay(10,scoreUp,1) elseif (isScoringUp == false) then display.remove(obj2) obj2 = nil end elseif (obj2.name == "ground" and obj1.name == "enemy") then if (isScoringUp == true) then display.remove(obj1) obj1 = nil timer.performWithDelay(10,scoreUp,1) elseif (isScoringUp == false) then display.remove(obj1) obj1 = nil end -- keys collision elseif (obj1.name == "key" and obj2.name == "ball") then if obj1 ~= nil then print("key collision") display.remove(obj1) obj1 = nil user.keys = user.keys + 1 loadsave.saveTable(user,"user.json") end elseif (obj1.name == "ball" and obj2.name == "key") then if obj2 ~= nil then display.remove(obj2) obj2 = nil user.keys = user.keys + 1 loadsave.saveTable(user,"user.json") end elseif event.object1.name == "key" and event.object2.name == "ground" then print("no keyyyyy") if event.object1 ~= nil then display.remove(event.object1) event.object1 = nil end elseif event.object2.name == "key" and event.object1.name == "ground" then print("no keyyyyy") if event.object2 ~= nil then display.remove(event.object2) event.object2 = nil end end -- end end end local function moveRight() if ball.x \< screenRight - ball.width \* 0.6 then transition.to(ball,{x = ball.x + 20,time =0}) else ball.x = screenRight - ball.width \*0.6 end end local function moveLeft() if ball.x \> screenLeft + ball.width \* 0.6 then transition.to(ball,{x = ball.x - 20,time = 0}) else ball.x = screenLeft + ball.width \*0.6 end end function onArrowTouch( event ) -- body print("arrow is running") if isArrowTouchEnable == true then if event.phase == "began" then timer\_left = timer.performWithDelay(20,moveLeft,0) elseif event.phase == "ended" then if timer\_left then timer.cancel(timer\_left) end end end end function onArrowTouch2( event ) -- body print("arrow is running") if isArrowTouchEnable == true then if event.phase == "began" then timer\_right = timer.performWithDelay(20,moveRight,0) elseif event.phase == "ended" then if timer\_right then timer.cancel(timer\_right) end end end end background = display.newImageRect (sceneGroup,"freePlay/images/Background image.png",800,1400) background.x = centerX ; background.y = centerY background.height = screenHeight; background.width = screenWidth -- check which ball to display if user.isRedEnabled == true then ball = display.newImageRect(sceneGroup,"upImages/ball/red-white-ball.png",75,75) elseif user.isYellowEnabled == true then ball = display.newImageRect(sceneGroup,"upImages/ball/yellow-white-ball.png",75,75) elseif user.isPurpleEnabled == true then ball = display.newImageRect(sceneGroup,"upImages/ball/purple-white-ball.png",75,75) elseif user.isOrangeEnabled == true then ball = display.newImageRect(sceneGroup,"upImages/ball/orange-white-ball.png",75,75) elseif user.isBlueEnabled == true then ball = display.newImageRect(sceneGroup,"upImages/ball/blue-white-ball.png",75,75) elseif user.isGreenEnabled == true then ball = display.newImageRect(sceneGroup,"upImages/ball/green-white-ball.png",75,75) end ball.x = centerX ball.y = centerY ball.name = "ball" physics.addBody(ball,"dynamic",{radius = ballRadius, bounce = 0.4,filter = {category = 1, maskBits = 1 }}) ball.isFixedRotation = true -- walls and ground leftWall = display.newRect (sceneGroup,0,0,20,screenHeight) leftWall.x = screenLeft - leftWall.width \* 0.5 leftWall.y = centerY leftWall:setFillColor(1,1,1) physics.addBody(leftWall,"static") rightWall = display.newRect (sceneGroup,0,0,20,screenHeight) rightWall.x = screenRight + rightWall.width \* 0.5 rightWall.y = centerY rightWall:setFillColor(1,1,1) physics.addBody(rightWall,"static") upWall = display.newRect(sceneGroup , 0,0,screenWidth, 10) upWall.x = centerX upWall.y = screenTop - upWall.height physics.addBody(upWall,"static", {filter = {category = 1, maskBits = 1 }}) ground = display.newRect (sceneGroup,0,0,screenWidth,20) ground.x = centerX ground.y = screenBottom + ground.height \* 0.5 ground:setFillColor(1,1,1) ground.name = "ground" physics.addBody(ground,"static") -- scoring module scoreText = display.newText(sceneGroup, "Score:0",0,0,\_font,40) scoreText.anchorX = 1 scoreText.x = screenRight - scoreText.width \* 0.2 scoreText.y = screenTop + scoreText.height -- arrow visuals leftArrow = widget.newButton { width = 190, height = 190, id = "left", defaultFile = "control/leftArrow.png", overFile = "control/leftArrow\_over.png" } leftArrow.x = screenLeft + leftArrow.width leftArrow.y = screenBottom - leftArrow.height sceneGroup:insert(leftArrow) leftArrow:addEventListener("touch",onArrowTouch) rightArrow = widget.newButton { width = 190, height = 190, id = "right", defaultFile = "control/rightArrow.png", overFile = "control/rightArrow\_over.png" } rightArrow.x = screenRight - rightArrow.width rightArrow.y = screenBottom - rightArrow.height sceneGroup:insert(rightArrow) rightArrow:addEventListener("touch",onArrowTouch2) midWall = display.newRect(sceneGroup, centerX, centerY, screenWidth, 20) midWall.isVisible = false midWall.name = "midwall" physics.addBody(midWall,"static",{filter = {category = 1,maskBits = 1}}) -- pause visuals blackBg = display.newRect(sceneGroup, centerX , centerY , screenWidth, screenHeight) blackBg.alpha = 0 blackBg:setFillColor(0) --white bg whiteBg = display.newRect(sceneGroup ,centerX , centerY , screenWidth, screenHeight) whiteBg.alpha = 0 whiteBg:setFillColor(1) -- pause visuals pauseBtn = widget.newButton { width = 100, height = 100, defaultFile = "freePlay/images/pause/pauseBtn.png", overFile = "freePlay/images/pause/pauseBtn\_over.png", onEvent = onPauseTouch } pauseBtn.x = screenLeft + pauseBtn.width pauseBtn.y = screenTop + pauseBtn.height \* 1.2 resumeBtn = widget.newButton { width = 250, height = 250 , defaultFile = "btn\_play.png", overFile = "btn\_play\_over.png", onEvent = onResumeTouch } resumeBtn.x = centerX resumeBtn.y = centerY - resumeBtn.height \* 0.5 resumeBtn.isVisible = false homeButton = widget.newButton { width = 150, height = 150, defaultFile = "freePlay/images/pause/btn\_home.png", overFile = "freePlay/images/gameOver/btn\_home\_over.png", onEvent = onHomeTouch } homeButton.x = centerX homeButton.y = centerY + homeButton.height homeButton.isVisible = false btn\_soundOn = widget.newButton { width = 70, height = 70, defaultFile = "freePlay/images/pause/soundOn.png", overFile = "freePlay/images/pause/soundOn\_over.png", onEvent = onSoundTouch } btn\_soundOn.x = centerX btn\_soundOn.y = centerY - resumeBtn.height \* 1.5 btn\_soundOn.isVisible = false btn\_soundOff = widget.newButton { width = 70, height = 70, defaultFile = "freePlay/images/pause/soundOff.png", overFile = "freePlay/images/pause/soundOff\_over.png", onEvent = onSoundTouch } btn\_soundOff.x = btn\_soundOn.x btn\_soundOff.y = btn\_soundOn.y btn\_soundOff.isVisible = false Runtime:addEventListener("enterFrame",sendEnemies) Runtime:addEventListener("collision",onCollision) sceneGroup:insert(pauseBtn) sceneGroup:insert(resumeBtn) sceneGroup:insert(homeButton) sceneGroup:insert(btn\_soundOn) sceneGroup:insert(btn\_soundOff) end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen local prevScene = composer.getSceneName("previous") if prevScene then composer.removeScene(prevScene) end end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

Please anyone help…

You’re calling physics.stop() at some point and not calling physics.start() by the next time you call physics.addBody()

I only placed physics.stop() when going to menu scene…So should i replace physics.stop() with physics.start()

Because replacing physics.stop with physics.start worked for me… 

  1. Why do you need to call physics stop?  Are you using that as some kind of forced pause?

  2. In any case, call physics.start() in the will phase of the scene show() function.

  3. In the future, you don’t need to post so much code, I know I didn’t read it.  Simply paste the important parts of you code and strip out anything that isn’t relevant.  I will say thanks for using a code block to format it though.

  1. I thought it will be helpful(improve performing) when stopping the physics engine while leaving scenes…

  2. Calling physics in the will phase of scene show didn’t worked…no physics is started when pasting that code into will phase 

  3. i will take care henceforth :) 

  1. IMHO using physics.stop() at all is bad - I’ve seen things go very wrong. The advice I’ve had (many years ago) was to use physics.pause() and physics.start() to get going again, never stop()

  2. I was going to mention this earlier - please, please, please don’t post your entire code base. The first step in figuring out a problem is boiling it down to the parts which matter. Posting your whole app to a forum will not get you much help.

  1. Ok Thanks :slight_smile:

  2. Extremely Sorry

Note: Stopping physics won’t make any different to performance if you’re not using the physics engine and you don’t have ‘stuff’ running in the background (which you should not anyways as that would be stuff running in a non-showing scene which would indicated a design error.)

The short of it, no need to stop physics.

Thank you so much :D 

Generally I recommend calling physics.start() then immediately calling physics.pause().  I personally don’t like executing code  the main chunk of the scene. I personally add those two calls to my scene:create(), but if you’re calling composer.removeScene() to unrequire the module it doesn’t really matter.

Pausing is important though. If you have any transition delays in the scene, say 500ms then your physics bodies have a half second of interaction before your scene is on the screen.  So either at the top of the scene or in scene:create()

physics.start() physics.pause()

Then in your scene’s “did” phase:

physics.start()

This will resume the simulation after the scene is on the screen. If you start after a pause in the “will” phase, you’re back into dealing with physics running before the transition completes.

Then in scene:hide()'s “will” phase call:

physics.pause()

to stop the simulation from running.

Rob

You’re calling physics.stop() at some point and not calling physics.start() by the next time you call physics.addBody()

I only placed physics.stop() when going to menu scene…So should i replace physics.stop() with physics.start()

Because replacing physics.stop with physics.start worked for me… 

  1. Why do you need to call physics stop?  Are you using that as some kind of forced pause?

  2. In any case, call physics.start() in the will phase of the scene show() function.

  3. In the future, you don’t need to post so much code, I know I didn’t read it.  Simply paste the important parts of you code and strip out anything that isn’t relevant.  I will say thanks for using a code block to format it though.

  1. I thought it will be helpful(improve performing) when stopping the physics engine while leaving scenes…

  2. Calling physics in the will phase of scene show didn’t worked…no physics is started when pasting that code into will phase 

  3. i will take care henceforth :) 

  1. IMHO using physics.stop() at all is bad - I’ve seen things go very wrong. The advice I’ve had (many years ago) was to use physics.pause() and physics.start() to get going again, never stop()

  2. I was going to mention this earlier - please, please, please don’t post your entire code base. The first step in figuring out a problem is boiling it down to the parts which matter. Posting your whole app to a forum will not get you much help.

  1. Ok Thanks :slight_smile:

  2. Extremely Sorry

Note: Stopping physics won’t make any different to performance if you’re not using the physics engine and you don’t have ‘stuff’ running in the background (which you should not anyways as that would be stuff running in a non-showing scene which would indicated a design error.)

The short of it, no need to stop physics.

Thank you so much :D