local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------- -- 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 physics = require( "physics" ) physics.start() math.randomseed( os.time()) -- physics.setDrawMode( "hybrid" ) physics.setGravity( 0,-.06 ) display.setStatusBar( display.HiddenStatusBar ) -- Initialize variables local lives = 1 local score = 0 local died = false local dronesTable = {} local helicoptersTable = {} local explosionsTable = {} local explosion local newDrone local laser local ship local gameLoopTimer local livesText local scoreText -- set up display groups local backGroup local mainGroup local uiGroup ---- ------------- function explosion(x,y) sheetInfo = require("explosion") -- init the image sheet explosion = graphics.newImageSheet( "explosion.png", sheetInfo:getSheet() ) sequenceData = { -- set up anmiation { name="explosion", -- name of the animation (used with setSequence) explosion=myImageSheet, -- the image sheet start=sheetInfo:getFrameIndex("1"), -- name of the first frame count=10, -- number of frames time=1500, -- speed loopCount=1, -- repeat }, } direction ="forward" explosion = display.newSprite( explosion, sequenceData ) explosion.myName = "explosion" explosion.x = x explosion.y = y explosion:setSequence("explosion") explosion:play() explosion.isVisible = true end -- -------------------------------------------------------------------------------------------------- function createDrone() physicsData = (require "droneshape").physicsData(1.0) droneInfo = require "drone" droneSheet = graphics.newImageSheet( "drone.png", droneInfo:getSheet() ) droneSeqData = { {name = "drone", frames = {1,2,3}, time=250, loopCount=0, loopDirection = "forward"}, } newDrone = display.newSprite( droneSheet, droneSeqData ) table.insert( dronesTable, newDrone ) newDrone.myName = "drone" newDrone:setSequence( "idle" ) newDrone:play() newDrone.isAlive = true physics.addBody( newDrone, "dynamic", { bounce= 0.0, isSensor= true , physicsData:get("drone")} ) whereFrom = math.random( 1 ) if ( whereFrom == 1 ) then -- From the right newDrone.x = display.contentWidth +70 newDrone.y = math.random(500) newDrone.speed = math.random(6, 12) newDrone.amp = math.random(20,60) newDrone.angle = math.random(20,60) newDrone.initY = newDrone.y end newDrone:setLinearVelocity( math.random( -1180,-530 ), math.random( -6,-4 ) ) end -- -------------------------------------------------------------------------------------------------- local function createHelicopter() local helicopterInfo = require "helicopter" local helicopterSheet = graphics.newImageSheet( "helicopter.png", helicopterInfo:getSheet() ) local helicopterSeqData = { {name = "heli", frames = {1,2,3}, time=150, loopCount=0, loopDirection = "forward"}, } local helicopter = display.newSprite( helicopterSheet, helicopterSeqData ) table.insert( helicoptersTable, helicopter ) physics.addBody( helicopter, "dynamic", {bounce= 0.0, isSensor= true} ) helicopter.myName = "helicopter" helicopter:setSequence( "helicopter" ) helicopter:play() local whereFrom = math.random( 1 ) if ( whereFrom == 1 ) then -- From the right helicopter.x = display.contentWidth +80 helicopter.y = math.random(200,500) helicopter.speed = math.random(6, 12) helicopter.amp = math.random(20,60) helicopter.angle = math.random(20,60) helicopter.initY = helicopter.y end helicopter:setLinearVelocity( math.random( -380,-330 ), math.random( -4,-2 ) ) end -- -------------------------------------------------------------------------------------------------- -- -------------------------------------------------------------------------------------------------- function fireLaser() -- Play fire sound! audio.play( fireSound ) laser = display.newImageRect( mainGroup, "laserfire.png", 45, 7 ) physics.addBody( laser, "static",{isSensor= true} ) laser.isBullet = true laser.myName = "laser" laser.collided = false laser.x = ship.x laser.y = ship.y laser:toBack() transition.to( laser, { x=2000, time=200, onComplete = function() display.remove( laser ) end }) end -- -------------------------------------------------------------------------------------------------- function activateShips(self, event) self:applyForce( 0, -10.0, ship.x, ship.y) end function touchScreen(event) if event.phase == "began" then ship.enterFrame = activateShips Runtime:addEventListener("enterFrame", ship) end if event.phase == "ended" then Runtime:removeEventListener("enterFrame", ship ) end end -- -------------------------------------------------------------------------------------------------- function showExplosion() --different name for less confusion, remember to change any references of the old name to this one. explosion(newDrone.x, newDrone.y) newDrone.isVisible = false --don't use explosion:play because it is already in the explosion(46-73) function end local function gameLoop() createDrone() for i = #dronesTable, 1, -1 do local thisDrone = dronesTable[i] if ( thisDrone.x \< -100 or thisDrone.x \> display.contentWidth + 100 or thisDrone.y \< -100 or thisDrone.y \> display.contentHeight + 100 ) then display.remove( thisDrone ) table.remove( dronesTable, i ) end end end gameLoopTimer = timer.performWithDelay( 500, gameLoop, 0 ) -- -------------------------------------------------------------------------------------------------- local function gameLoop() -- Create new asteroid createHelicopter() for i = #helicoptersTable, 1, -1 do local thisHelicopter = helicoptersTable[i] if ( thisHelicopter.x \< -100 or thisHelicopter.x \> display.contentWidth + 100 or thisHelicopter.y \< -100 or thisHelicopter.y \> display.contentHeight + 100 ) then display.remove( thisHelicopter ) table.remove( helicoptersTable, i ) end end end gameLoopTimer = timer.performWithDelay( 1000, gameLoop, 0 ) -- -------------------------------------------------------------------------------------------------- function onCollision( event ) if ( event.phase == "began" ) then local obj1 = event.object1 local obj2 = event.object2 if ( ( obj1.myName == "laser" and obj2.myName == "drone" ) or ( obj1.myName == "drone" and obj2.myName == "laser" ) ) then if ( died == false) then died = true showExplosion() else -- Remove both the laser and drone display.remove( obj1 ) display.remove( obj2 ) for i = #dronesTable, 1, -1 do if ( dronesTable[i] == obj1 or dronesTable[i] == obj2 ) then table.remove( dronesTable, i ) break end end end end end end -- -------------------------------------------------------------------------------------------------- local function onCollision1( event ) if ( event.phase == "began" ) then local obj1 = event.object1 local obj2 = event.object2 if ( ( obj1.myName == "laser" and obj2.myName == "helicopter" ) or ( obj1.myName == "helicopter" and obj2.myName == "laser" ) ) then if ( died == false ) then died = true else -- Remove both the laser and drone display.remove( obj1 ) display.remove( obj2 ) for i = #helicoptersTable, 1, -1 do if ( helicoptersTable[i] == obj1 or helicoptersTable[i] == obj2 ) then table.remove( helicoptersTable, i ) break end end end end end end -- ----- -- ----------------------------------------------------------------------------------- -- 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 physics.pause() -- Temporarily pause the physics engine -- Set up display groups backGroup = display.newGroup() -- Display group for the background image sceneGroup:insert( backGroup ) -- Insert into the scene's view group mainGroup = display.newGroup() -- Display group for the ship, asteroids, lasers, etc. sceneGroup:insert( mainGroup ) -- Insert into the scene's view group uiGroup = display.newGroup() -- Display group for UI objects like the score sceneGroup:insert( uiGroup ) -- Insert into the scene's view group -- -------------------------------------------------------------------------------------------------- local background = display.newImageRect( backGroup, "background.png", 960, 720 ) background.x = display.contentCenterX background.y = display.contentCenterY -- -------------------------------------------------------------------------------------------------- local city1 = display.newImageRect(backGroup, "city1.png", 900, 500 ) city1.x = 450 city1.y = display.contentHeight-247 -- -------------------------------------------------------------------------------------------------- local city2 = display.newImageRect(backGroup, "city1.png", 860, 500 ) city2.x = 1345 city2.y = display.contentHeight-247 -- -------------------------------------------------------------------------------------------------- function scrollCity(self,event) if self.x \< - 335 then self.x = 1345 else self.x = self.x - 2 end end -- -------------------------------------------------------------------------------------------------- city1.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city1) city2.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city2) -- -------------------------------------------------------------------------------------------------- local laserButton = display.newImageRect(mainGroup, "laserButton.png", 130, 130 ) laserButton.x = 850 laserButton.y = 650 ship = display.newImageRect( mainGroup, "alienship.png", 70, 55 ) ship.myName = "ship" ship.x = 140 ship.y = 195 physics.addBody( ship, "dynamic", { radius=30, bounce= 0}) ship.gravityScale = -1185 Runtime:addEventListener( "touch" , touchScreen) laserButton:addEventListener("tap", fireLaser) fireSound = audio.loadSound( "audio/fire.wav" ) musicTrack = audio.loadStream( "audio/game\_theme\_back ground.wav" ) 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 physics.start() Runtime:addEventListener( "collision", onCollision ) Runtime:addEventListener( "collision", onCollision1 ) audio.play( musicTrack, { channel=1, loops=-1 } ) 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 Runtime:removeEventListener( "collision", onCollision ) physics.pause() composer.removeScene( "game" ) end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view audio.dispose( explosionSound ) audio.dispose( fireSound ) audio.dispose( musicTrack ) end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene
Can someone help me out? I’m creating this shooting game, but can’t seem to get the explosion effect right can someone look at my lines of code and tell me what I’m I doing wrong?