Hello everyone…
I am developing a flappy bird style game where the ship (I’ve used ship instead bird) can fire bullets in the direction of touch on the screen as well as it can be kept upward by tapping on a button in left corner. Until now everything is working nicely. The game flow is nice and is working. But when ship collides with bad objects the scene is changes and it goes to menu (as I want). But soon after it shows me this error.
(the error is at line 391)
this is that line…
gameloopTimer = timer.performWithDelay(500,gameloop,0)
here’s code …
local composer = require( "composer" ) local scene = composer.newScene() local physics = require "physics" local gameLoopTimer -------------------------------------------- ------------------------------------------- local function destroyScene() composer.gotoScene( "menu", { time=800, effect="crossFade" }) composer.removeScene("level1") end local function endGame() timer.performWithDelay(200,function() destroyScene() end) end -------------------------------------------- ------------------------------------------- local score = 3 local score2 = 0 local scoreText local scoreText2 scoreText = display.newText("ballsleft:"..score,800,15,native.systemFontBold, 30) scoreText:setFillColor (1 ) scoreText2 = display.newText("score:"..score2,220,15,native.systemFontBold, 30) scoreText2:setFillColor (1 ) local function updateText() scoreText.text = "ballsleft: " .. score end local function updateText() scoreText.text = "score: " .. score2 end ---------------------------------------- --------------------------------------- local function onCollision( event ) if ( event.phase == "began" ) then local obj1 = event.object1 local obj2 = event.object2 if (obj1.surfaceType == "good" and obj2.surfaceType == "bad" ) then display.remove( obj1 ) elseif ( (obj1.surfaceType == "ship" ) and obj2.surfaceType == "good" ) then display.remove( obj2 ) score2 = score2 + 1 scoreText2.text = "score: " .. score2 score = score + 2 scoreText.text = "ballsleft: " .. score elseif ( (obj1.surfaceType == "ship" ) and (obj2.surfaceType == "bad" )) then display.remove( obj1 ) endGame() display.remove(scoreText) display.remove(scoreText2) end end end ---------------------------------------- --------------------------------------- local loop local loop1 local angleof local angleof1 local move = 5000 loop = 60 loop1 = 240 angleof = 180 angleof1 = 0 local function speedup() move = move - 300 end timer.performWithDelay(5000000,speedup,0) local function realgame() loop = math.random(120,180) loop1 = math.random(20,190) end local function realgame1() angleof = (math.random(50,180)) angleof1 = (math.random(50,180)) end local my = function() return realgame() end timer.performWithDelay(1000,my,1) local my1 = function() return realgame1() end timer.performWithDelay(1000000,my1,1) local n = math.random(140,230) ----------------------------------------- -------------------------------------------- local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX ------------------------------------------ ------------------------------------------ function scene:create( event ) math.randomseed(system.getTimer()) local sceneGroup = self.view physics.setDrawMode( "normal") end ------------------------------------------- -------------------------------------------- function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then elseif phase == "did" then physics.start() physics.setGravity(0, 12) ---------------------------- -- display rcect which are translated and having no influence of physics-- ---------------------------- local speed local rect1 = display.newImageRect(sceneGroup,"sl.png",math.random(40,80),math.random(40,80)) rect1.x = math.random(40,480) rect1.y = math.random(40,300) rect1.alpha = .1 rect1.speed = 1 local rect2 = display.newImageRect(sceneGroup,"sl.png",math.random(40,80),math.random(40,80)) rect2.x = math.random(40,480) rect2.y = math.random(40,300) rect2.alpha = .1 rect2.speed = 2 local rect3 = display.newImageRect(sceneGroup,"sl.png",math.random(40,80),math.random(40,80)) rect3.x = math.random(40,480) rect3.y = math.random(40,300) rect3.alpha = .1 rect3.speed = 3 local rect4 = display.newImageRect(sceneGroup,"sl.png",math.random(40,80),math.random(40,80)) rect4.x = math.random(40,480) rect4.y = math.random(40,300) rect4.alpha = .1 rect4.speed = 4 local rect5 = display.newImageRect(sceneGroup,"sl.png",math.random(10,20),math.random(10,20)) rect5.x = math.random(40,480) rect5.y = math.random(40,300) rect5:setFillColor(math.random(0.1,.9),math.random(0.1,.9),math.random(0.1,.9)) rect5.alpha = .5 rect5.speed = 5 local rect6 = display.newImageRect(sceneGroup,"sl.png",math.random(10,20),math.random(10,20)) rect6.x = math.random(40,480) rect6.y = math.random(40,300) rect6:setFillColor(math.random(0.1,.9),math.random(0.1,.9),math.random(0.1,.9)) rect6.alpha = .5 rect6.speed = 4 local rect7 = display.newImageRect(sceneGroup,"sl.png",math.random(10,20),math.random(10,20)) rect7.x = math.random(40,480) rect7.y = math.random(40,300) rect7:setFillColor(math.random(0.1,.9),math.random(0.1,.9),math.random(0.1,.9)) rect7.alpha = .5 rect7.speed = 3 local rect8 = display.newImageRect(sceneGroup,"sl.png",math.random(10,20),math.random(10,20)) rect8.x = math.random(40,480) rect8.y = math.random(40,300) rect8:setFillColor(math.random(0.1,.9),math.random(0.1,.9),math.random(0.1,.9)) rect8.alpha = .5 rect8.speed = 2 local rect9 = display.newImageRect(sceneGroup,"sl.png",math.random(10,20),math.random(10,20)) rect9.x = math.random(40,480) rect9.y = math.random(40,300) rect9:setFillColor(math.random(0.1,.9),math.random(0.1,.9),math.random(0.1,.9)) rect9.alpha = .5 rect9.speed = 1 ------------------------------------ ----------------------------------- local function movebackground1 ( self, event) if self.x \< -80 then self.x = math.random(550,800) self.y = math.random(0,300) self.width = math.random(40,80) self.height = math.random(40,80) self:rotate(0,179) else self.x = self.x - self.speed end end rect1.enterFrame = movebackground1 Runtime:addEventListener("enterFrame",rect1) rect2.enterFrame = movebackground1 Runtime:addEventListener("enterFrame",rect2) rect3.enterFrame = movebackground1 Runtime:addEventListener("enterFrame",rect3) rect4.enterFrame = movebackground1 Runtime:addEventListener("enterFrame",rect4) ----------------------------------- ----------------------------------- local function movebackl ( self, event) if self.x \< -80 then self.x = math.random(550,800) self.y = math.random(0,300) self.width = math.random(10,20) self.height = math.random(10,20) self:setFillColor(math.random(0.1,.9),math.random(0.1,.9),math.random(0.1,.9)) else self.x = self.x - self.speed end end rect5.enterFrame = movebackl Runtime:addEventListener("enterFrame",rect5) rect6.enterFrame = movebackl Runtime:addEventListener("enterFrame",rect6) rect7.enterFrame = movebackl Runtime:addEventListener("enterFrame",rect7) rect8.enterFrame = movebackl Runtime:addEventListener("enterFrame",rect8) rect9.enterFrame = movebackl Runtime:addEventListener("enterFrame",rect9) ------------------------------------- ------------------------------------ local backgroun = display.newImageRect( scene.view,"background.jpg",700,480) backgroun.x = display.contentCenterX backgroun.y = display.contentCenterY backgroun.alpha = .4 backgroun.fill.effect = "filter.blurGaussian" backgroun.fill.effect.horizontal.blurSize = 10 backgroun.fill.effect.horizontal.sigma = 70 backgroun.fill.effect.vertical.blurSize = 10 backgroun.fill.effect.vertical.sigma = 70 local platform = display.newImageRect(scene.view,"platform.png",900,50) platform.x = 240 platform.y = 330 local platformu = display.newImageRect(scene.view,"platformu.png",780,50) platformu.x = 240 platformu.y = -10 platformu:rotate(180) ------------------------------------ --display ship and its functions of firing bullet when tapped on anywhere on screen-- ----------------------------------- function angleOfPoint( pt ) local x, y = pt.x, pt.y local radian = math.atan2(y,x) local angle = radian\*180/math.pi if angle \< 0 then angle = 360 + angle end return angle end -------------------------------- function angleBetweenPoints( a, b ) local x, y = b.x - a.x, b.y - a.y return angleOfPoint( { x=x, y=y } ) end function rotateTo( point, degrees ) local x, y = point.x, point.y local theta = math.rad(degrees) local pt = { x = x \* math.cos(theta) - y \* math.sin(theta), y = x \* math.sin(theta) + y \* math.cos(theta) } return pt end --------------------------------- function fireBullet(source, angle) local bullet = display.newImageRect(sceneGroup,"bu.png",20,20) bullet.x, bullet.y, bullet.rotation = source.x, source.y, angle physics.addBody( bullet ) bullet.surfaceType = "bullet" bullet.isSensor = true bullet.gravityScale = 0 local direction = rotateTo( {x=5,y=0}, angle ) bullet:applyForce( direction.x, direction.y, bullet.x, bullet.y ) end ----------------------------------- local ship = display.newImageRect(sceneGroup,"cannon.png",50,50) ship.x = 50 ship.y = 150 physics.addBody( ship,"dynamic") ship.surfaceType = "ship" ship.isSensor = true ---------------------------- function touch( e ) local angle = angleBetweenPoints( ship, e ) ship.rotation = angle-0 if (e.phase == "began") then elseif (e.phase == "moved") then elseif (e.phase == "ended") then fireBullet(ship, angle) score = score - 1 scoreText.text = "ballsleft: " .. score if score == 0 then print("zero") end end return true end Runtime:addEventListener("touch",touch) ------------------------------------ local newrect = display.newRect(sceneGroup,40,280,50,50) newrect:setFillColor(.5,.8) ---------------------------------- function activateJets(self,event) self:setLinearVelocity( 0, -120 ) end local function touchListener2( event ) if ( event.phase == "began" ) then event.target.alpha = 0.5 display.getCurrentStage():setFocus( event.target ) ship.enterFrame = activateJets Runtime:addEventListener("enterFrame",ship) elseif ( event.phase == "ended" or event.phase == "cancelled" ) then event.target.alpha = 1 display.getCurrentStage():setFocus( nil ) Runtime:removeEventListener("enterFrame",ship) end return true end newrect:addEventListener( "touch", touchListener2 ) Runtime:addEventListener( "collision", onCollision ) ------------------------------------- --start gameloop of triangles and towers ------------------------------------- local function gameloop () local rand = math.random(0,3) if (rand \< 1) then local toweru1 = display.newImageRect(sceneGroup,"toweru1.png",math.random(30,60),math.random(50,130)) toweru1.x = 600 toweru1.y = 60 toweru1.alpha = .8 toweru1:rotate( math.random(0,360)) physics.addBody( toweru1, "dynamic" ) toweru1.gravityScale = 0 toweru1.isSensor = true toweru1.surfaceType = "bad" toweru1:setFillColor (.2,.5,.9) transition.moveTo( toweru1, { x=-400, y=loop, time=move } ) elseif (rand \< 2) then local toweru11 = display.newImageRect(sceneGroup,"tower4.png",math.random(50,80),math.random(50,130)) toweru11.x = 600 toweru11.y = 240 toweru11.alpha = .8 toweru11:setFillColor(.5,.8,.9) toweru11:rotate( 180 ) toweru11:rotate( math.random(180,360) ) physics.addBody( toweru11, "dynamic" ) toweru11.gravityScale = 0 toweru11.isSensor = true toweru11.surfaceType = "bad" toweru11:setFillColor (.2,.5,.9) transition.moveTo( toweru11, { x=-400, y=loop1, time=move } ) elseif(rand\<3) then local rand = math.random(0,3) if ( rand \< 1) then local triangle = display.newImageRect(sceneGroup,"triangle.png",70,70) triangle.x = 600 triangle.y = 70 triangle:rotate( 180 ) physics.addBody( triangle, "dynamic" ) triangle.gravityScale = 0 triangle.isSensor = true triangle.surfaceType = "good" transition.moveTo( triangle, { x=-400, y=loop, time=move} ) elseif ( rand \< 2) then local triangle = display.newImageRect(sceneGroup,"triangle.png",70,70) triangle.x = 600 triangle.y = 245 physics.addBody( triangle, "dynamic" ) triangle.gravityScale = 0 triangle.isSensor = true triangle.surfaceType = "good" transition.moveTo( triangle, { x=-400, y=loop1, time=move } ) elseif ( rand \< 3) then local tower = display.newImageRect(sceneGroup,"sl.png",math.random(20,50),math.random(50,180)) tower.x = 600 tower.y = n tower.alpha = .8 physics.addBody(tower, "dynamic" ) tower.gravityScale = 0 tower.isSensor = true tower.surfaceType = "good" transition.moveTo( tower, { x=-400, y=n, time=move } ) end end end gameloopTimer = timer.performWithDelay(500,gameloop,0) ---\*\*\*error line \*\*\*---- gameloop() end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then physics.pause() elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view physics.stop() package.loaded[physics] = nil physics = nil end --------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene