CoronaSDK场景管理库:Composer library (中)

药药切克闹,场景例子举一个吆~~

首先是main.lua

local composer = require( "composer" )

print("test1")
composer.gotoScene("scene1")

其次是scene1.lua

--场景1
local composer = require( "composer" )

local scene = composer.newScene()

-- -----------------------------------------------------------------------------------------------------------------
-- 所有监听函数之外的代码都只会被执行一次,除非调用composer.removeScene()
-- -----------------------------------------------------------------------------------------------------------------

-- 本地引用的声明放在这里

-- -------------------------------------------------------------------------------
local img
local myTouchListener

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

    local sceneGroup = self.view

    -- 初始化场景
    -- 例如:add display objects to "sceneGroup", add touch listeners, etc.
    img = display.newImageRect("1.png", 300, 300)    
    sceneGroup:insert(img)

    img:addEventListener("touch", myTouchListener)

    print("scene1:create")
end

function myTouchListener( event )
    -- body
    if(event.phase == "began") then
        composer.gotoScene("scene2")
    end
    return true
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).
        img:translate(100,100)
        img:setFillColor(1,0,1)

        print("scene1:show(will)")
    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.
        print("scene1:show(did)")
    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.
        print("scene1:hide(will)")
    elseif ( phase == "did" ) then
        -- Called immediately after scene goes off screen.
        print("scene1:hide(did)")
    end
end


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

    local sceneGroup = self.view

    -- 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.
    img:removeEventListener("touch", myTouchListener)

    img:removeSelf()
    img = nil

    print("scene1:destroy")
end


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

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

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

return scene

最后是scene2.lua

--场景2
local composer = require( "composer" )

local scene = composer.newScene()

-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called.
-- -----------------------------------------------------------------------------------------------------------------

-- local forward references should go here

-- -------------------------------------------------------------------------------
local topAlignAxis = 200
local options1 = 
{
    text = "The quick brown fox jumped over the lazy dog.",
    x = 90,
    width = 120,     --required for multi-line and alignment
    font = native.systemFont,
    fontSize = 18
}
local myText1

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

    local sceneGroup = self.view

    -- Initialize the scene here.
    -- Example: add display objects to "sceneGroup", add touch listeners, etc.
    myText1 = display.newText( options1 )
    sceneGroup:insert(myText1)

    print("scene2:create")
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).
        myText1:setFillColor( 1, 0, 0 )
        myText1.anchorY = 0
        myText1.y = topAlignAxis

        print("scene2:show(will)")
    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.
        transition.to( myText1, { time=5000, y=300, transition=easing.inOutExpo, onComplete = function ()
            -- body
            composer.gotoScene("scene1")
        end} )

        print("scene2:show(did)")
    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.
        transition.cancel(myText1)

        print("scene2:hide(will)")
    elseif ( phase == "did" ) then
        -- Called immediately after scene goes off screen.
        print("scene2:hide(did)")
    end
end


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

    local sceneGroup = self.view

    -- 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.
    transition.cancel(myText1)
    myText1:removeSelf()
    myText1 = nil

    print("scene2:destroy")
end


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

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

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

return scene

第一个场景有一个图片,点击图片,则切换到第二个场景。第二个场景有一个动画,动画完成后直接切回第一个场景。

调试界面显示结果如下,请仔细体会:

 

这时候,我们在scene2.lua中修改一下,在动画开始前,把scene1场景给删除掉,看看是否会调用destroy以及重新create:

执行结果如下:

posted @ 2015-01-20 09:19  弯弓射月  阅读(1228)  评论(0编辑  收藏  举报