Hi There, I'm just starting out with Gideros. So the thing is I have a project which uses Scenemanager for managing scenes. There are currently two scenes Menu and Level01. To identify those scenes I added a text field on both scenes, the issue is while switching the scenes, text field on the previous scene is not clearing. Just want to know is this the intended behaviour or am I missing something. Thanks in advance. (I'm attaching my project along with this thread. )
main.luarequire "scenemanager"
require "easing"
scenemanager = SceneManager.new(
{
["menu"] = Menu,
["level01"] = Level01,
}
)
stage:addChild(scenemanager)
scenemanager:changeScene("menu") |
Menu.luaMenu = Core.class(Sprite)
function Menu:init()
-- BG
application:setBackgroundColor(0x12ffff)
-- LISTENERS
self:addEventListener("enterBegin", self.onTransitionInBegin, self)
self:addEventListener("enterEnd", self.onTransitionInEnd, self)
self:addEventListener("exitBegin", self.onTransitionOutBegin, self)
self:addEventListener("exitEnd", self.onTransitionOutEnd, self)
end
-- GAME LOOP
function Menu:onEnterFrame(e)
self.font = Font.new("Pixellari.txt", "Pixellari.png") -- you need to add your bitmap font
self.textfield = TextField.new(self.font, "some text")
stage:addChild(self.textfield)
self.textfield:setText("Menu") -- change the text
self.textfield:setPosition(100, 100)
end
function Menu:onExitFrame(e)
print("Exiting.....")
end
-- EVENT LISTENERS
function Menu:onTransitionInBegin()
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
function Menu:onTransitionInEnd()
self:myKeysPressed()
end
function Menu:onTransitionOutBegin()
self:removeEventListener(Event.ENTER_FRAME, self.onExitFrame, self)
end
function Menu:onTransitionOutEnd()
end
-- KEYS HANDLER
function Menu:myKeysPressed()
self:addEventListener(Event.KEY_DOWN, function(e)
-- for mobiles and desktops
if e.keyCode == KeyCode.BACK or e.keyCode == KeyCode.ESC then
scenemanager:changeScene("level01", 1, SceneManager.moveFromRight, easing.outBack)
end
end)
end |
Level01.luaLevel01 = Core.class(Sprite)
function Level01:init()
-- BG
application:setBackgroundColor(0x1234AA)
-- LISTENERS
self:addEventListener("enterBegin", self.onTransitionInBegin, self)
self:addEventListener("enterEnd", self.onTransitionInEnd, self)
self:addEventListener("exitBegin", self.onTransitionOutBegin, self)
self:addEventListener("exitEnd", self.onTransitionOutEnd, self)
end
-- GAME LOOP
function Level01:onEnterFrame(e)
self.font = Font.new("Pixellari.txt", "Pixellari.png") -- you need to add your bitmap font
self.textfield = TextField.new(self.font, "some text")
stage:addChild(self.textfield)
self.textfield:setText("Level 01") -- change the text
self.textfield:setPosition(150, 150)
end
function Level01:onExitFrame(e)
stage:removeChild(textField)
end
-- EVENT LISTENERS
function Level01:onTransitionInBegin()
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
function Level01:onTransitionInEnd()
self:myKeysPressed()
end
function Level01:onTransitionOutBegin()
self:removeEventListener(Event.ENTER_FRAME, self.onExitFrame, self)
end
function Level01:onTransitionOutEnd()
end
-- KEYS HANDLER
function Level01:myKeysPressed()
self:addEventListener(Event.KEY_DOWN, function(e)
-- for mobiles and desktops
if e.keyCode == KeyCode.BACK or e.keyCode == KeyCode.ESC then
scenemanager:changeScene("menu", 1, SceneManager.moveFromLeft, easing.outBack)
end
end)
end |
Comments
also you do not need to create and add a textfield at each frame, this part should be moved to the init (actually given that you never change the text, even the setText could be moved to the init part and you do not even need onEnterFrame):
all the same goes for your Menu scene.
Likes: MoKaLux
Fragmenter - animated loop machine and IKONOMIKON - the memory game
main.lua this looks fine
Next: Menu.lua
Why? because you don't create new in the game loop it's very expensive regarding performance.
You should create new in the init function of your class like so:
Finally to have the expecting result you should not do:
Conclusion: you don't add textfield directly to stage but instead to the class it belongs to.
Likes: MoKaLux
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Likes: MoKaLux, antix
Likes: lulman, MoKaLux