在Lua显示场面的比赛

时间:2015-05-10 20:51:18

标签: lua corona

我正在为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

1 个答案:

答案 0 :(得分: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()。也需要插入场景的视图组,或者在更改场景之前必须删除它们。