hey guys,
This is just a gist of my game…it is not actually the game…i have game in which the player has to save the ball from boxes that will spawn from top of the screen… so the score increase by 50 points when a player dodges one box…for example: 50,100,150,200…but when game overs and when i go back to the menu scene and restart the level, the score starts increasing by 100 points…for example : 100,200,300,400 etc. I want it to increase just 50 points…
Here is my code… it is the scene_play.lua:
--Description: we need to drag the ball and save it from being collided with enemy boxes that will spawn --from above. local composer = require( "composer" ) local scene = composer.newScene() -- Calling the physics library local physics = require "physics" physics.start() physics.setGravity(0,9.8) -- This are constant variables from main.lua file centerX = display.contentCenterX centerY = display.contentCenterY screenLeft = display.screenOriginX screenWidth = display.contentWidth - screenLeft \* 2 screenRight = screenLeft + screenWidth screenTop = display.screenOriginY screenHeight = display.contentHeight - screenTop \* 2 screenBottom = screenTop + screenHeight -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- --local forward references local leftWall, rightWall, ground, ball, gameOverBox, gameOverBack, gameOverReload local enemy -- math random local mRand = math.random --local timers local spawnTimer --local score text local scoreText local score = 0 --local isScoringUp is set to true so that when boxes collides with ground, scoreUp function will be called local isScoringUp = true -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- creating a drag function to drag the ball local function dragMe(event) 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 -- function of going back to menu local function onBackTouch( event ) -- body -- go to menu scene composer.gotoScene("scene\_menu","fade") end --This function is called when player's ball collides with enemy boxes local function gameOver(e) --gameIsActive = false --stop the physics and cancel the timer to stop spawning enemy boxes physics.stop() timer.cancel(spawnTimer) --start the transition of game over display and set its alpha to 1 transition.to(gameOverBox,{alpha = 1 , time =1000}) transition.to(gameOverBack,{alpha = 1 , time =1000}) transition.to(gameOverReload,{alpha = 1 , time =1000}) transition.to(scoreText,{ x = gameOverBox.x - 80 , y = gameOverBox.y, time = 1000 }) scoreText:setFillColor(1) scoreText:toFront() --set the score to zero score = 0 --set isScoringUp to true so that the next time the player plays this game, the score will increase when enemy boxes collides with the gorund local function isScoringUpStart() isScoringUp = true end timer.performWithDelay(3000, isScoringUpStart, 1) end -- This function is called when enemy boxes collides with the ground local function scoreUp(event) score = score + 50 scoreText.text = "score : " .. score scoreText.x = screenLeft end -- This function is called when physics objects collides local function onCollision(event) if event.phase=="began" then -- If Enemy boxes hits the ground, remove the enemy boxes ------- if event.object1.name =="ground" and event.object2.name =="enemy" then event.object2:removeSelf() -- if isScoringUp is set to true, increase the score(call the scoreUp function) if isScoringUp == true then scoreUp() end elseif event.object1.name == "enemy" and event.object2.name == "ground" then event.object1:removeSelf() if isScoringUp == true then scoreUp() end -- If enemy boxes collides with the ball, remove the ball and call gameOver function -- and set isScoringUp to false so that when enemy boxes collides with ground, score doesn't increase -- elseif event.object1.name =="enemy" and event.object2.name =="ball" then event.object2:removeSelf() gameOver() isScoringUp = false elseif event.object1.name == "ball" and event.object2.name == "enemy"then event.object1:removeSelf() gameOver() isScoringUp = false end end end -- 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 -- create the backGround, add walls so that ball doesn't go off screen, add a ground so that ball bounces -- create a ball, create the game over display and set its alpha to zero so that it is not visible when playig the game -- local play\_background = display.newRect(sceneGroup,screenLeft, screenTop, screenWidth,screenHeight) play\_background.anchorX = 0 play\_background.anchorY = 0 leftWall = display.newRect(sceneGroup,0,0,screenWidth \* 0.05, screenHeight) leftWall.x = screenLeft - (leftWall.width \* 0.5) leftWall.y = centerY leftWall.name = "leftWall" leftWall:setFillColor(0) physics.addBody(leftWall,"static") rightWall = display.newRect(sceneGroup,0,0,screenWidth \* 0.05, screenHeight) rightWall.x = screenRight + (rightWall.width \* 0.5) rightWall.y = centerY rightWall.name = "rightWall" rightWall:setFillColor(0) physics.addBody(rightWall,"static") ground = display.newRect(sceneGroup,0,0,screenWidth, screenHeight \* 0.05) ground.x = centerX ground.y = screenBottom + (ground.height\*0.5) ground.name = "ground" ground:setFillColor(0) physics.addBody(ground,"static") ball = display.newCircle(sceneGroup,centerX,centerY,30) ball:setFillColor(1,0,0) ball.name = "ball" physics.addBody(ball,{radius=30}) ball:addEventListener("touch",dragMe) scoreText = display.newText(sceneGroup,"score : 0", 0,display.screenOriginY +30, native.systemFont,50) scoreText.anchorX = 0 scoreText.x = screenLeft scoreText:setTextColor(1,0,0) gameOverBox = display.newRect(sceneGroup,0,0,screenWidth\*0.5,screenHeight \* 0.3) gameOverBox.x = centerX gameOverBox.y = centerY gameOverBox:setFillColor(1,0,0) gameOverBox.alpha = 0 gameOverBack = display.newText(sceneGroup,"Back",0,0,\_FONT,50) gameOverBack.x = gameOverBox.x - gameOverBack.width gameOverBack.y = gameOverBox.y + gameOverBox.height\*0.5 - gameOverBack.height gameOverBack:addEventListener("touch", onBackTouch) gameOverBack.alpha = 0 gameOverReload = display.newText(sceneGroup, "Reload",0,0,\_FONT,50) gameOverReload.x = gameOverBox.x + gameOverReload.width \* 0.6 gameOverReload.y = gameOverBox.y + gameOverBox.height\*0.5 - gameOverBack.height gameOverReload.alpha = 0 --function to spawn enemies local function spawnEnemy(enemy) enemy = display.newRect(sceneGroup,0,0,120,110) enemy.x = mRand(screenLeft , screenRight - enemy.width) enemy.y = display.screenOriginY - 100 enemy.anchorX = 0 enemy.name ="enemy" enemy:setFillColor(mRand(),mRand(),mRand()) physics.addBody(enemy,"dynamic",{bounce = -0.9,density = 1.5}) end spawnTimer = timer.performWithDelay(500,spawnEnemy,-1) Runtime:addEventListener("collision",onCollision) 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 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
any help would be appreciated
Thanks