无法从Game Over返回菜单。分数无法在屏幕上显示在游戏中

时间:2014-11-12 10:12:31

标签: lua corona

我对电晕很新。我从网上学到了所有这些都是我制作的。游戏看起来很好,但是当屏幕上的游戏显示时,我回到菜单场景的按钮不起作用,并且玩家获得的分数无法显示给..

这是我的代码...有人请帮助我..我应该更改或添加什么代码? 谢谢。非常感谢任何帮助。

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 = 5
local mossie
local bee
local shade
local gameOverScreen
local winScreen
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 gameOver = function()

    composer.removeScene("easy")
    physics.pause()

    --audio.play(gameOverSound)
    background = display.newImageRect( "Images/bg.jpg", display.contentWidth, display.contentHeight )
    background.anchorX = 0
   background.anchorY = 0
   background.x, background.y = 0, 0
    gameOverScreen = display.newImage("Images/gameover.png",400,300)
    gameOverScreen.x = 160
    gameOverScreen.y = 240
    gameOverScreen.alpha = 0
    transition.to(gameOverScreen,{time=500,alpha=1})

    --total.isVisible = true
    total.text="Score : "..count.touch
    total.x = 160
    total.y = 400
    --total:setTextColor(000000)

    botwall.isVisible = false
    mossie.isVisible = false
    bee.isVisible = false
    life.isVisible = false
    countdown.isVisible = false
    pauseBtn.isVisible = false
    resumeBtn.isVisible = false

    local myButton = widget.newButton
    {
    left = 100,
    top = 100,
    id = "myButton",
    label = "Menu",
    onEvent = handleButtonEvent
    }

    myButton.isVisible = true

 end

local function handleButtonEvent( event )
    if ( "ended" == event.phase ) then
        composer.gotoScene ("menu")
    end
end

local function countDown(e)
    time_remain = time_remain-1
    countdown.text = time_remain

    if time_remain == 0 then
      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
                    gameOver()
                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
        gameOver()
    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)

   dropMossie = timer.performWithDelay( 2000 , newMossie, -1 )
   dropBee = timer.performWithDelay( 1800 , newBee, -1)
   gametmr = timer.performWithDelay(1000, countDown, -1)

   -- 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


      -- 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

2 个答案:

答案 0 :(得分:0)

我建议为gameOverScreen创建另一个作曲家场景(就像你发布的这些代码块一样)。然后做一个composer.gotoScene(gameOver)阅读更多here。为了得分,我建议你制作像this这样的东西。

答案 1 :(得分:0)

在创建按钮小部件后声明函数 handleButtonEvent 。您可以通过移动有关窗口小部件代码的功能来获得所需的结果。

    local function handleButtonEvent( event )
        if ( "ended" == event.phase ) then
            composer.gotoScene ("menu")
        end
    end

    local myButton = widget.newButton
    {
        left = 100,
        top = 100,
        id = "myButton",
        label = "Menu",
        onEvent = handleButtonEvent
    }
    myButton.isVisible = true
end