Tuto Gideros Game Template1 Part 4 Transitions
Transitions
For our game, we will transition between scenes using a transition scene.
The transitions scene will receive as parameter the next scene to load. The transitions scene will have a MovieClip to simulate transition effects using the tweens we declared in the init.lua file.
The transitions can be interrupted by pressing some keys.
Imho it makes sense to create the file in the scenes folder. You can call the file "transitions.lua". Here it is:
Transitions = Core.class(Sprite)
function Transitions:init(xnextscene)
self.nextscene = xnextscene
-- a movieclip for transitions (you can replace the pixel with a bitmap, ...)
local pixel = Pixel.new(g_ui_theme.pixelcolorup, 1, myappwidth, myappheight)
local mc = MovieClip.new{{ -- you choose the duration, the effects, ...
1, 50, pixel, -- 64, 32
{
x = { myappleft, myappleft, },
y = { 0, myappheight, tweens[31] }, -- tweens[random(#tweens)]
alpha = { 1, 0, tweens[19] }, -- tweens[random(#tweens)]
}
}}
mc:play() -- play only once
-- order
self:addChild(mc)
-- listeners
mc:addEventListener(Event.COMPLETE, function()
self:gotoScene(self.nextscene)
end)
-- let's go
self:myKeysPressed()
end
-- cancel the transition
function Transitions:myKeysPressed()
self:addEventListener(Event.KEY_DOWN, function(e)
if e.keyCode == KeyCode.ESC or e.keyCode == KeyCode.ENTER or e.keyCode == KeyCode.SPACE or e.keyCode == g_keyaction1 then
self:gotoScene(self.nextscene)
end
end)
end
-- scene navigation
function Transitions:gotoScene(xnextscene)
self:removeAllListeners()
for i = stage:getNumChildren(), 1, -1 do
stage:removeChildAt(i)
end collectgarbage()
stage:addChild(xnextscene) -- next scene, xnextscene = Class.new()
end
Code comments
In the init function we pass the next scene we want to load, eg.:
Transitions.new(Menu.new())
Here we use a Pixel to cover the whole screen and animate it using a MovieClip. Feel free to make your own transitions.
We can press a key to cancel the transition.
Finally, before switching to the new scene we remove all the current listeners and children to keep the memory consumption low. collectgarbage() helps us with this.
We are ready to load the next scene!
Next?
Logically the next scene we see is the menu scene?
Prev.: Tuto Gideros Game Template1 Part 3 Main
Next: Tuto Gideros Game Template1 Part 5 Menu
Tutorial - Gideros Game Template1