Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Scenemanager not clearing textfield of previous scenes - Newbie question - Gideros Forum

Scenemanager not clearing textfield of previous scenes - Newbie question

lulmanlulman Member
edited November 2020 in General questions
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.lua
require "scenemanager"
require "easing"
 
scenemanager = SceneManager.new(
	{
		["menu"] = Menu,
		["level01"] = Level01,
	}
)
stage:addChild(scenemanager)
scenemanager:changeScene("menu")
Menu.lua
Menu = 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.lua
Level01 = 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

Likes: MoKaLux

zip
zip
First.zip
15K
+1 -1 (+1 / -0 ) Share on Facebook

Comments

  • keszeghkeszegh Member
    edited November 2020
    you don't need onExitFrame function (it is never run anyway in your code), but you should have the rest like this:
    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.onEnterFrame, self)
    end

    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):
    function Level01:init()
    	-- BG
    	application:setBackgroundColor(0x1234AA)
    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:setPosition(150, 150)
     
    	-- 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.textfield:setText("Level 01") -- change the text
     
    end

    all the same goes for your Menu scene.

    Likes: MoKaLux

    +1 -1 (+1 / -0 ) Share on Facebook
  • keszegh said:

    you don't need onExitFrame function (it is never run anyway in your code), but you should have the rest like this:

    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.onEnterFrame, self)
    end

    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):
    function Level01:init()
    	-- BG
    	application:setBackgroundColor(0x1234AA)
    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:setPosition(150, 150)
     
    	-- 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.textfield:setText("Level 01") -- change the text
     
    end

    all the same goes for your Menu scene.
    I changed the code on my side like you suggested. But still issue is there. Currently the output is like below screenshot


    1.png
    805 x 605 - 5K
  • MoKaLuxMoKaLux Member
    edited November 2020 Accepted Answer
    first of all welcome to the forum :) We will try to help you :smile:
    main.lua this looks fine :)
    Next: Menu.lua
    -- 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
    This does not look fine :neutral:
    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:
    function Menu:init()
    	-- BG
    	application:setBackgroundColor(0x12ffff)
    	-- your new functions go here
    	self.font = Font.new("Pixellari.txt", "Pixellari.png")
    	self.textfield = TextField.new(self.font, "some text")
    	stage:addChild(self.textfield)
    	self.textfield:setText("Menu") -- change the text
    	self.textfield:setPosition(100, 100)	
    	-- 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)
    end
    You should do the same for the Game01 class :smile:


    Finally to have the expecting result you should not do:
    stage:addChild(self.textfield)
    but
    self:addChild(self.textfield)
    That should fix your issue.

    Conclusion: you don't add textfield directly to stage but instead to the class it belongs to.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • Here I post your project modified.
    zip
    zip
    FirstV2.zip
    16K
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • @MoKaLux , good find, i've overlooked "stage:addChild(self.textfield)"

    Likes: MoKaLux

    +1 -1 (+1 / -0 ) Share on Facebook
  • @MoKaLux @keszegh Kool :). It worked. Thank you guys for your time and effort. I really learned something new.

    Likes: MoKaLux, antix

    +1 -1 (+2 / -0 ) Share on Facebook
  • @lulman, keep it up, Gideros is really fantastic :)

    Likes: lulman, MoKaLux

    +1 -1 (+2 / -0 ) Share on Facebook
Sign In or Register to comment.