Hey guys I have an app that I am working on and I have it set up so their is a main menu and the game and a lose scene. But the physics in the main level only works sometimes. Any idea of what I should do?
level1.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() -- include Corona's "physics" library local physics = require "physics" physics.start(); physics.pause() -------------------------------------------- function scene:create( event ) -- Called when the scene's view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. local movement = 5 local playerhlth = 7 local enemyhlth = 10 local sceneGroup = self.view -- create a grey rectangle as the backdrop local sidebar = display.newRect(-50,0,50,600) physics.addBody(sidebar,"static") local bg = display.newRect(250,100,1000,1000) bg:setFillColor(0,0,1) local square = display.newRect(50,150,30,30) square:setFillColor(1,0,0) physics.addBody(square,"dynamic") local enemy = display.newRect(400,50,30,30) physics.addBody(enemy,"dynamic") enemy.category = "enemy" -- Make character jump function square:touch(event) if(event.phase == "began") then square:setLinearVelocity( 0, -200 ) end end local function attackenemy( event ) enemyhlth = enemyhlth- 1 print("main player is attacking") print("enemy health is",enemyhlth) end enemy:addEventListener("tap",attackenemy) square:addEventListener("touch",up) local function enemyattack( self,event ) if (event.phase == "began") then if ( event.other.category == "enemy" ) then --subtract health, etc. playerhlth = playerhlth - 1 print("you are being attacked") print("your health is",playerhlth) local urhealth = display.newText(playerhlth,square.x,square.y,25,25) urhealth:setFillColor(0,1,0) transition.to(urhealth,{time= 4000,x=0,y=-500}) end end if playerhlth == 0 then composer.removeScene("level1") composer.gotoScene("lose") square:removeSelf() end end square.collision = enemyattack square:addEventListener( "collision", square ) function loopObject() local objectDown= function() transition.to(enemy, { time=700,x= square.x, y=square.y, onComplete=loopObject }) end transition.to(enemy, { time=700, y=square.y, onComplete=objectDown }) end loopObject() local ground = display.newRect(300,300,800,75) physics.addBody(ground,"static") local rightarrow = display.newImage("arrowright.png",370,290) rightarrow:scale(0.1,0.1) local function moveright( event ) square.x = square.x + movement end rightarrow:addEventListener("touch",moveright) local leftarrow = display.newImage("leftarrow.png",200,290) leftarrow:scale(0.1,0.1) local function moveleft( event ) square.x = square.x - movement end leftarrow:addEventListener("touch",moveleft) -- all display objects must be inserted into group sceneGroup:insert(bg) sceneGroup:insert(ground) sceneGroup:insert(enemy) sceneGroup:insert(sidebar) sceneGroup:insert(leftarrow) sceneGroup:insert(rightarrow) sceneGroup:insert(square) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. physics.start() end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) physics.stop() elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene