Corona SDK MOSQUITO杀戮游戏

时间:2014-11-11 04:43:56

标签: android lua corona

我非常了解电晕和开发游戏。

问题1:当时间到/用户点击bee / life = 0并显示一个名为“游戏结束”的新场景时,如何停止游戏? ?

问题2:每当我暂停游戏,然后点击恢复,蚊子数量增加很多。如何改变?

local composer = require(" composer")     local scene = composer.newScene()

local physics = require("physics")
local widget = require "widget"
physics.start()
rand = math.random( 20 )

local slap_sound = audio.loadSound("Sound/slap2.mp3")
local ow = audio.loadSound("Sound/ow.mp3")
local buttonSound = audio.loadSound("Sound/sound2.mp3")
local background
local back
local count={total1=0,total=0,touch=0,life=3}
local total
local time_remain = 30
local mossie
local bee
local shade
local gameOverScreen
local winScreen
local gameIsActive = false
local countdown
local life
local pauseBtn
local resumeBtn
local gametmr
---------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE
-- unless "composer.removeScene()" is called.
---------------------------------------------------------------------------------
local function countDown(e)
    time_remain = time_remain-1
    countdown.text = time_remain

    if time_remain == 0 then
        composer.gotoScene("gameover")
    end
end

local pauseGame = function(e)
    if(e.phase=="ended") then
        audio.play(buttonSound)
        physics.pause()
        timer.pause(gametmr)
        pauseBtn.isVisible = false
        resumeBtn.isVisible = true
        return true
    end
end

local resumeGame = function(e)
    if(e.phase=="ended") then
        audio.play(buttonSound)
        physics.start()
        timer.resume(gametmr)
        pauseBtn.isVisible = true
        resumeBtn.isVisible = false
        return true
    end
end

local collisionListener=function(self,event)
    if(event.phase=="began")then
        if(event.other.type=="mossie")then
            audio.play(ow)
            count.life=count.life-1
                --if(count.life==0) then

                --end   
            event.other:removeSelf()
            event.other=nil
        else
            event.other:removeSelf()
            event.other=nil
        end
    end 
end

function onTouch(mossie)
    audio.play(slap_sound)
    count.touch=count.touch+1
    total.text="Score : "..count.touch
    mossie.target:removeSelf()
end

function killIt(e)
    if(e.phase == "ended") then

    end
end

local bottomWall = function()
    botwall=display.newImage("Images/tangan.png")
    botwall.x = 160
    botwall.y = 500
    botwall:setFillColor(22,125,185,255)
    botwall.type="botwall"
    botwall.collision=collisionListener
    physics.addBody(botwall,"static",{ density=100.0, friction=0.0, bounce=0.0} )
    botwall:addEventListener("collision",botwall)
end

local function newMossie(event)    
    total.text="Score : "..count.touch
    life.text="Life : "..count.life
    mossie = display.newImage("Images/biasa.png") 
    mossie.x = 60 + math.random( 160 )
    mossie.y = -100
    mossie.type="mossie"
    mossie:setFillColor(255,0,0)
    physics.addBody( mossie, { density=0.3, friction=0.2, bounce=0.5} )
    mossie.name = "mossie"
    mossie:addEventListener("touch",onTouch)
end 

local function newBee(event)
    bee = display.newImage("Images/lebah.png")
    bee.x = 60 + math.random( 160 )
    bee.y = -100
    bee.type="other"
    physics.addBody( bee, { density=1.4, friction=0.3, bounce=0.2} )
    bee:addEventListener("touch",killIt)
end


-- local forward references should go here

---------------------------------------------------------------------------------

-- "scene:create()"
function scene:create( event )

   local sceneGroup = self.view

   background = display.newImageRect( "Images/bg.jpg", display.contentWidth, display.contentHeight )
   background.anchorX = 0
   background.anchorY = 0
   background.x, background.y = 0, 0

   total=display.newText("Score : 0",display.contentWidth * 0.5, 20, "Arial", 26)
   total:setTextColor(000000)

   countdown=display.newText(time_remain ,display.contentWidth * 0.9, 20, "Arial", 26)
   countdown:setTextColor(000000)

   life = display.newText("Life : 3 " ,display.contentWidth * 0.5, 50, "Arial", 26)
   life:setTextColor(000000)

   pauseBtn = display.newImage("Images/pause.png")
   pauseBtn.x = display.contentWidth * 0.1
   pauseBtn.y = display.contentHeight - 450

   resumeBtn = display.newImage("Images/playb.png") 
   resumeBtn.x = display.contentWidth * 0.1
   resumeBtn.y = display.contentHeight - 450

   botwall=display.newImage("Images/tangan.png")
   botwall.x = 160
   botwall.y = 500
   botwall:setFillColor(22,125,185,255)
   botwall.type="botwall"
   botwall.collision=collisionListener
   physics.addBody(botwall,"static",{ density=100.0, friction=0.0, bounce=0.0} )


   sceneGroup:insert(background)
   sceneGroup:insert(total)
   sceneGroup:insert(countdown)
   sceneGroup:insert(life)
   sceneGroup:insert(pauseBtn)
   sceneGroup:insert(resumeBtn)
   sceneGroup:insert(botwall)

   resumeBtn.isVisible = false
   pauseBtn:addEventListener("touch", pauseGame)
   resumeBtn:addEventListener("touch", resumeGame)
   botwall:addEventListener("collision",botwall)

   -- Initialize the scene here.
   -- Example: add display objects to "sceneGroup", add touch listeners, etc.
end

-- "scene:show()"
function scene:show( event )

   local sceneGroup = self.view
   local phase = event.phase



   if ( phase == "will" ) then

      -- Called when the scene is still off screen (but is about to come on screen).
   elseif ( phase == "did" ) then
   dropMossie = timer.performWithDelay( 2000 , newMossie, -1 )
   dropBee = timer.performWithDelay( 1800 , newBee, -1)
   gametmr = timer.performWithDelay(1000, countDown, -1)


      -- Called when the scene is now on screen.
      -- Insert code here to make the scene come alive.
      -- Example: start timers, begin animation, play audio, etc.
   end
end

-- "scene:hide()"
function scene:hide( event )

   local sceneGroup = self.view
   local phase = event.phase



   if ( phase == "will" ) then




      -- Called when the scene is on screen (but is about to go off screen).
      -- Insert code here to "pause" the scene.
      -- Example: stop timers, stop animation, stop audio, etc.
   elseif ( phase == "did" ) then


      -- Called immediately after scene goes off screen.
   end
end

-- "scene:destroy()"
function scene:destroy( event )

   local sceneGroup = self.view
   physics.stop()
   timer.cancel(gametmr)
    pauseBtn:removeEventListener("touch", pauseGame)
   resumeBtn:removeEventListener("touch", resumeGame)
   botwall:removeEventListener("collision",botwall)
   bee:removeEventListener("touch",killIt)
   mossie:removeEventListener("touch",onTouch)
   -- Called prior to the removal of scene's view ("sceneGroup").
   -- Insert code here to clean up the scene.
   -- Example: remove display objects, save state, etc.
end

---------------------------------------------------------------------------------

-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

---------------------------------------------------------------------------------

return scene

1 个答案:

答案 0 :(得分:0)

是的,为gameOver创建一个新场景。确保你停止所有计时器,将它们排除在外。你在这个场景中使用的所有内容都是为了避免内存泄漏。只需创建一个可以执行这些操作的功能,以便您重复使用它。对于问题2,你没有暂停dropBee和dropMossie这就是为什么当你恢复游戏时它们会成倍增加,因为计时器还在滴答作响。