2015-05-10 60 views
0

我正在爲Lua中的編程課程開發遊戲。但是,我無法讓game_over場景顯示。顯然它實際上是在展示,但在所有事情的背後。但是,我需要實際的場景完全消失,所以當我回到主屏幕時,我可能會從頭開始再次開始遊戲。我嘗試過不同的方法,但似乎沒有任何工作。有什麼建議麼?在Lua中展示Game Over場景

這是我的代碼。

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

--- 
-- 
-- play.lua 
-- 
--------------------------------------------------------------------------------- 

local sceneName = ... 

local composer = require("composer") 

-- Load scene with same root filename as this file 
local scene = composer.newScene(sceneName) 

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


local widget = require("widget") 


local back 
local score = 0 -- **************** 


-- *************************** 
local function finish_alien() 
    if score > 1 then 
     composer.showOverlay("game_over") 
     audio.pause(1) 
    end 
end 





local function tap_back(event) 
    composer.gotoScene("home", {effect="slideLeft", time=400}) 
end 










-------------------------------------------------------------------- 
-- Create a couple loaded sounds to be used for game events. 
-------------------------------------------------------------------- 
local explosion_sound = audio.loadSound("images/sound_k.mp3") 
local miss_sound = audio.loadSound("images/sound_fail.wav") 
-------------------------------------------------------------------- 

--The code above creates two loaded sounds: explosion_sound and miss_sound. The next step is to find a place in the code to play these sounds. 

--For the miss_sound add the following code. 

-------------------------------------------------------------------- 
-- use this to handle touch events on the background. This will catch touches that 
-- miss an alien. 
-------------------------------------------------------------------- 
local function touch_background(event) 
    if event.phase == "began" then 
     audio.play(miss_sound) 
    end 
end 

Runtime:addEventListener("touch", touch_background) 
-------------------------------------------------------------------- 






local alien_sheet = graphics.newImageSheet("images/aliens2.png", {width=30, height=50, numFrames=40}) 
local explosion_sheet = graphics.newImageSheet("images/explosion-boom.png", {width=50, height=50, numFrames=10}) 

local function remove_explosion(event) 
    if event.phase == "ended" then 
    display.remove(event.target) 
    end 
end 

local function make_explosion() 
    local explosion_sprite = display.newSprite(explosion_sheet, {start=1, count=40, loopCount=1}) 
    explosion_sprite:addEventListener("sprite", remove_explosion) 
    explosion_sprite:play() 
    return explosion_sprite 
end 



function remove_alien(alien_sprite) 
    display.remove(alien_sprite) 
end 
------------------------------------------------------------------------------------- 
-- Touch event occurs on alien sprite 
------------------------------------------------------------------------------------- 
local function touch_alien(event) 
    if event.phase == "began" then     
     local explosion_sprite = make_explosion() 
     explosion_sprite.x = event.target.x   
     explosion_sprite.y = event.target.y   
     remove_alien(event.target)  
     audio.play(explosion_sound)  -- Play explosion sound 
     score = score + 1 -- score ************* 
     finish_alien() -- ********************** 
    end 
    return true -- prevent touch event propagation 
end 
------------------------------------------------------------------------------------- 






local function reset_zep(zep) 
    zep.x = -100 
    zep.y = math.random(50, 250) 
    transition.to(zep, {x=display.contentWidth + 100, time=10000, onComplete= reset_zep}) 
end 






------------------------------------------------------------------------------------- 
-- Tried to make the Game Over scene appear with this function, but I don't know if it's actually correct. 
------------------------------------------------------------------------------------- 














function scene:create(event) 
    local group = self.view 


    local background = display.newImageRect("images/sky.jpg", 600, 600) 
    background.alpha = 1 
    background.x = 240 
    background.y = 160 



    local city = display.newImageRect("images/skyline.png", 480, 320) 
    background.alpha = 1 
    city.x = 240 
    city.y = 260 


    local zep1 = display.newImageRect("images/zeppelin.png", 225, 100) 
    zep1.alpha = 1 
    zep1.x = -200 
    zep1.y = 100   
    transition.to(zep1, {x=display.contentWidth + 100, alpha=1, time=80000, onComplete=reset_zep}) 

    local zep2 = display.newImageRect("images/zeppelin.png", 75, 34) 
    zep2.alpha = 1 
    zep2.x = -250 
    zep2.y = 160 
    transition.to(zep2, {x=display.contentWidth + 100, time=80000, delay=2000, onComplete=reset_zep}) 


    -------------------------------------------------------------------- 
    -- This block of code adds a streaming sound and a button to play and resume that sound. 
    -------------------------------------------------------------------- 
    local background_sound = audio.loadStream("images/sound_bg.wav") 
    audio.play(background_sound, {channel=1, loops=-1}) 

    local play_pause_button = display.newImage("images/speaker.png") 
    play_pause_button.x = 32 
    play_pause_button.y = 32 

    local sound_state = "on" 

    local function set_sound_state(event) 
    if event.phase == "began" then 
     if sound_state == "on" then 
      sound_state = "off" 
      audio.pause(1) 
     else 
      sound_state = "on" 
      audio.resume(1) 
     end 
    end 
    return true 
    end 

    play_pause_button:addEventListener("touch", set_sound_state) 
    -------------------------------------------------------------------- 



    ------------------------------------------------------------------------------------- 
    -- This function makes aliens from the sprite sheet 
    ------------------------------------------------------------------------------------- 
    local function make_alien() 
     local n = math.random(0, 3) * 10 + 1 
     local alien_sprite = display.newSprite(alien_sheet, {start=n, count=10, loopCount=0, time=2000}) 

      alien_sprite:play() 
      alien_sprite.x = -40 
      alien_sprite.y = math.random(40, 280) 
      transition.to(alien_sprite, {time=3300, x=520, onComplete=remove_alien}) 
     -- Add a touch event to each alien sprite. 
     alien_sprite:addEventListener("touch", touch_alien) 

     if score > 1 then 
       alien_sprite:removeSelf() 
     end 

    end 

    local alien_timer = timer.performWithDelay(1000, make_alien, 0) 





end 









function scene:show(event) 
    local group = self.view 


end 





function scene:hide(event) 
    local group = self.view 

    composer.removeScene("play") 

end 


function scene:destroy(event) 
    local group = self.view 

end 

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

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

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

return scene 

回答

0

如果你打算使用像Composer這樣的場景管理器,你需要讓它管理你的場景。例如,查看場景:create()函數。您創建了所有這些display.newImage()和display.newImageRect()調用,但是您從不將它們放到場景的「視圖」(即它是顯示組)中。

每個項目應該是這樣:

local background = display.newImageRect("images/sky.jpg", 600, 600) 
background.alpha = 1 
background.x = 240 
background.y = 160 
group:insert(background) 

然後你的場景管理器可以打開和關閉屏幕,你想轉換的東西。您還在場景之外創建對象:create()。也需要插入到場景的視圖組中,或者在更改場景之前必須將其刪除。