画面遷移サンプル全体コード
main.lua
composer = require("composer")
TopScene = require("TopScene")
MainScene = require("MainScene")
_W = display.contentWidth
_H = display.contentHeight
composer.gotoScene(TopScene.name, nil, 0)
TopScene.lua
local this = composer.newScene()
this.name = "TopScene"
function this:create(event)
local sceneGroup = this.view
this.background = display.newRect(sceneGroup, _W/2, _H/2, _W, _H)
this.background:setFillColor(0.5, 0.5, 0.5)
this.nameText = display.newText({
parent = sceneGroup,
text = this.name,
x = _W/2,
y = _H/2,
width = _W,
height = 100,
font = native.systemFont,
fontSize = 60,
align = "center"
})
this.nameText:addEventListener("tap", this.onNameTextTap)
end
function this:show(event)
local phase = event.phase
if "did" == phase then
end
end
function this:hide(event)
local phase = event.phase
if "will" == phase then
end
end
function this:destroy(event)
this.nameText:removeEventListener("tap", this.onNameTextTap)
this.removeAll()
end
this.removeAll = function()
local sceneGroup = this.view
for i = sceneGroup.numChildren, 1, -1 do
local child = sceneGroup[i]
child.parent:remove(child)
child = nil
end
end
this.onNameTextTap = function(event)
composer.gotoScene(MainScene.name, "crossFade", 500)
timer.performWithDelay(500, 1, function()
composer.removeScene(this.name)
end)
end
this:addEventListener("create", this)
this:addEventListener("show", this)
this:addEventListener("hide", this)
this:addEventListener("destroy", this)
return this
MainScene.lua
local this = composer.newScene()
this.name = "MainScene"
function this:create(event)
local sceneGroup = this.view
this.background = display.newRect(sceneGroup, _W/2, _H/2, _W, _H)
this.background:setFillColor(0.0, 0.5, 0.5)
this.nameText = display.newText({
parent = sceneGroup,
text = this.name,
x = _W/2,
y = _H/2,
width = _W,
height = 100,
font = native.systemFont,
fontSize = 60,
align = "center"
})
this.nameText:addEventListener("tap", this.onNameTextTap)
end
function this:show(event)
local phase = event.phase
if "did" == phase then
end
end
function this:hide(event)
local phase = event.phase
if "will" == phase then
end
end
function this:destroy(event)
this.nameText:removeEventListener("tap", this.onNameTextTap)
this.removeAll()
end
this.removeAll = function()
local sceneGroup = this.view
for i = sceneGroup.numChildren, 1, -1 do
local child = sceneGroup[i]
child.parent:remove(child)
child = nil
end
end
this.onNameTextTap = function(event)
composer.gotoScene(TopScene.name, "fade", 500)
timer.performWithDelay(500, 1, function()
composer.removeScene(this.name)
end)
end
this:addEventListener("create", this)
this:addEventListener("show", this)
this:addEventListener("hide", this)
this:addEventListener("destroy", this)
return this