Corona SDK 換頁的範例

---------------------------------------------------------
--main.lua
---------------------------------------------------------
-- Hide the status bar.
display.setStatusBar(display.HiddenStatusBar)

-- 必須引入 controller module
local composer = require("composer")

-- 載入指定的頁面s1
composer.gotoScene( "s1", "fade", 400 )



---------------------------------------
--s1.lua
---------------------------------------
-- Hide the status bar.
display.setStatusBar(display.HiddenStatusBar)

local composer = require( "composer" )

--這裡也是重點之一,物件名稱要注意,像下列宣告scene,則最後一行的return 必須回傳scene
local scene_s1 = composer.newScene()

local function local function ButtonNext_Handle()
    composer.gotoScene("s2","slideLeft", 800 )        
end

local function ButtonApple_Handle()
     a = display.newImage("apple1.png")
     a.x=50
     a.y=50
--將image的物件a放到sceneGroup,如果沒有這一行,換頁後image還會留在畫面上,不會清除
     sceneGroup:insert(a)
end

function scene_s1:create()
    --這裡必須宣告一個view,我個人覺得比較像是容器,往後所有的物件都要insert到sceneGroup,
--這樣換頁時,執行removeSence才會把所有包在容器的物件一併刪除
--另外self.view只能在Create Event中建立,所以不要加上local,讓它變成全域變數,
--ButtonApple_Handle中產生的物件才能加到sceneGroup容器中
    sceneGroup = self.view

local ButtonNext = widget.newButton
    {
        label = "TEST",
        labelColor = { default={ 0, 0, 0 } },
        fontSize = 20,
        onEvent = ButtonNext_Handle,
        emboss = false,
        --properties for a rounded rectangle button...
        shape="roundedRect",
        width = 100,
        height = 50,
        cornerRadius = 10,
        fillColor = { default={ 0.8, 0.1, 0.4, 1 }, over={ 1, 0.1, 0.7, 0.4 } }
    }

    ButtonNext.x = 150
    ButtonNext.y = 50

    local ButtonApple = widget.newButton
    {
        label = "apple",
        labelColor = { default={ 0, 0, 0 } },
        fontSize = 20,
        onEvent = ButtonApple_Handle,
        emboss = false,
        --properties for a rounded rectangle button...
        shape="roundedRect",
        width = 100,
        height = 50,
        cornerRadius = 10,
        fillColor = { default={ 0.8, 0.1, 0.4, 1 }, over={ 1, 0.1, 0.7, 0.4 } }
    }

    ButtonApple.x = 250
    ButtonApple.y = 50
    
--把物件都加到sceneGroup,如果沒有加入的物件在換頁時不會被清除
    sceneGroup:insert(ButtonApple)
    sceneGroup:insert(ButtonNext)

end


--宣告S1 Create Event
scene_s1:addEventListener( "create", scene_s1)


--最後一行是重點,必須回傳名稱scene_s1(因為上面宣告scene_s1),這樣才能正確識別Screne Name
return scene_s1



---------------------------------------
--s2.lua
---------------------------------------
-- Hide the status bar.
display.setStatusBar(display.HiddenStatusBar)

local composer = require( "composer" )

local scene_s2 = composer.newScene()

function scene_s2:show()
    --移除s1的頁面
    composer.removeScene("s1")
end


--宣告S1 Create Event
scene_s2:addEventListener( "show", scene_s2)

--最後一行是重點,必須回傳名稱scene_s2(因為上面宣告scene_s2),這樣才能正確識別Screne Name
return scene_s2