Their are 10 game.lua scenes . And 10 restart.lua . The first scene is start.lua when the user touches the screen they go into game.lua . If they complete game.lua they go into restart.lua which makes then touch the screen to go into the next level (game2.lua) . And then that keeps going until they beat the game . If they run out of time , then they will go into another scene and restart the game over . Do I have to add the timer into the create scene to have it removed to ? I didn’t do this in other apps and they work perfectly fine .
I’ll post my whole code to see if I can give you a better understanding of what’s happening .
game.lua:
-- requires local physics = require "physics" physics.start() local composer = require( "composer" ) local scene = composer.newScene() local background function scene:create(event) local screenGroup = self.view local randomImage = math.random(1,41) local background = display.newImageRect("images/background"..randomImage..".jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) CreateWalls(screenGroup,1) display.setDefault("fillColor", 0, 1, 1) -- create image of a ball Ball = display.newCircle(100, 100, 10) physics.addBody(Ball, "dynamic", {friction=2}) Ball:applyLinearImpulse(-.05, .05, 0, 0) screenGroup:insert(Ball) end local options = { effect = "fade", time = 400 } function SetDefaultAnchor( pos ) display.setDefault("anchorX", pos.x) display.setDefault("anchorY", pos.y) end function CreateWalls( ScreenGroup, BorderWidth ) -- make the math easier local OldAnchor = {x = display.getDefault(anchorX), y = display.getDefault(anchorY) } SetDefaultAnchor({x=0, y=0}) local Height = display.contentHeight -- + (2 \* BorderWidth) local Width = display.contentWidth -- + (2 \* BorderWidth) local leftWall = display.newRect( ScreenGroup, 0, 0, BorderWidth, Height) -- this is where the error is local rightWall = display.newRect( ScreenGroup, Width - BorderWidth, 0, BorderWidth, Height) local ceiling = display.newRect( ScreenGroup, 0, 0, Width, BorderWidth) local floor = display.newRect( ScreenGroup, 0, Height-BorderWidth, Width, BorderWidth) physics.addBody (leftWall, "static", {bounce = 0.7, friction = 2}) physics.addBody (rightWall, "static", {bounce = 0.0, friction = 2}) physics.addBody (ceiling, "static", {bounce = 0.8, friction = 2}) physics.addBody (floor, "static", {bounce = 0.0, friction = 2}) -- restore previous defaults SetDefaultAnchor(OldAnchor) end local timeLimit = 6 timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 24) timeLeft:setTextColor(255,0,0) function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then display.remove(timeLeft) timer.cancel(timerr) composer.gotoScene("maxtime",options) end end timerr = timer.performWithDelay(1000,timerDown,timeLimit) function onBallTap( event ) timer.cancel(timerr) composer.gotoScene("restart",options) end function scene:show(event) local sceneGroup = self.view composer.removeScene( "start" ) Ball:addEventListener( "tap", onBallTap ) end function scene:hide(event) local sceneGroup = self.view end function scene:destroy(event) end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destroy", scene) return scene
restart.lua:
-- requires local composer = require( "composer" ) local scene = composer.newScene() -- background local background function scene:create(event) local screenGroup = self.view local background = display.newImageRect("nextlevel.jpg",display.contentWidth,display.contentHeight) screenGroup:insert(background) end local options = { effect = "flip", time = 400 } local function touchScreen( event ) if event.phase == "began" then display.remove(timeLeft) composer.removeScene( "game" ) composer.gotoScene( "game2", options ) end end function scene:show(event) if event.phase == "did" then Runtime:addEventListener("touch", touchScreen) end end function scene:hide(event) if event.phase == "did" then Runtime:removeEventListener("touch", touchScreen) end end function scene:destroy(event) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene