Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Scene Manager question — Gideros Forum

Scene Manager question

I'm trying to learn how to use the SceneManager, so I made this:

main.lua:
sceneManager = SceneManager.new({
 
	["menu"] = Menu,
 
})
 
stage:addChild(sceneManager)
sceneManager:changeScene("menu")
menu.lua:
Menu = Core.class(Sprite)
 
function Menu:init()
	local botao = Bitmap.new(Texture.new("button.jpg"))
	stage:addChild(botao)
end
The button itself doesn't matter. I just putted something on the screen to try the scene management.
The real problem is this:
Uploading finished.
main.lua:1: attempt to index nil with 'new'
stack traceback:
	main.lua:1: in function
Am I doing this right? The tutorial i'm following is 5 years old, so if i'm doing something wrong please tell me!
Thanks in advance!!!

Comments

  • MoKaLuxMoKaLux Member
    edited April 2023 Accepted Answer
    Since it is a plugin I think you need to initialize it:
    require "scenemanager"
    Other than that, your code looks good.

    Let me share how I do it :)

    init.lua:
    -- plugins
    require "scenemanager"
    require "easing"
    -- globals
    screenwidth, screenheight = application:get("screenSize") -- the actual user's screen size, yes!
    myappleft, myapptop, myappright, myappbot = application:getLogicalBounds()
    myappwidth, myappheight = myappright - myappleft, myappbot - myapptop
    ...
    main.lua:
    -- scene manager
    scenemanager = SceneManager.new(
    	{
    		["menu"] = Menu,
    		["levelX"] = LevelX, -- maker
    		["levelY"] = LevelY, -- viewer
    	}
    )
    stage:addChild(scenemanager)
    scenemanager:changeScene("menu")
    -- tables
    transitions = {
    	SceneManager.moveFromRight, -- 1
    	SceneManager.moveFromLeft, -- 2
    	SceneManager.moveFromBottom, -- 3
    	SceneManager.moveFromTop, -- 4
    	SceneManager.moveFromRightWithFade, -- 5
    	SceneManager.moveFromLeftWithFade, -- 6
    	SceneManager.moveFromBottomWithFade, -- 7
    	SceneManager.moveFromTopWithFade, -- 8
    	SceneManager.overFromRight, -- 9
    	SceneManager.overFromLeft, -- 10
    	SceneManager.overFromBottom, -- 11
    	SceneManager.overFromTop, -- 12
    	SceneManager.overFromRightWithFade, -- 13
    	SceneManager.overFromLeftWithFade, -- 14
    	SceneManager.overFromBottomWithFade, -- 15
    	SceneManager.overFromTopWithFade, -- 16
    	SceneManager.fade, -- 17
    	SceneManager.crossFade, -- 18
    	SceneManager.flip, -- 19
    	SceneManager.flipWithFade, -- 20
    	SceneManager.flipWithShade, -- 21
    }
    easings = {
    	easing.inBack, -- 1
    	easing.outBack, -- 2
    	easing.inOutBack, -- 3
    	easing.inBounce, -- 4
    	easing.outBounce, -- 5
    	easing.inOutBounce, -- 6
    	easing.inCircular, -- 7
    	easing.outCircular, -- 8
    	easing.inOutCircular, -- 9
    	easing.inCubic, -- 10
    	easing.outCubic, -- 11
    	easing.inOutCubic, -- 12
    	easing.inElastic, -- 13
    	easing.outElastic, -- 14
    	easing.inOutElastic, -- 15
    	easing.inExponential, -- 16
    	easing.outExponential, -- 17
    	easing.inOutExponential, -- 18
    	easing.linear, -- 19
    	easing.inQuadratic, -- 20
    	easing.outQuadratic, -- 21
    	easing.inOutQuadratic, -- 22
    	easing.inQuartic, -- 23
    	easing.outQuartic, -- 24
    	easing.inOutQuartic, -- 25
    	easing.inQuintic, -- 26
    	easing.outQuintic, -- 27
    	easing.inOutQuintic, -- 28
    	easing.inSine, -- 29
    	easing.outSine, -- 30
    	easing.inOutSine, -- 31
    }
    menu.lua:
    Menu = Core.class(Sprite)
     
    function Menu:init()
    	-- BG
    	application:setBackgroundColor(0x227178)
    	-- btns
    	-- btnMaker = Button.new(...)
    	-- btns listeners
    	btnMaker:addEventListener("clicked", function() self:gotoScene("levelX") end)
    	-- 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
     
    -- EVENT LISTENERS
    function Menu:onTransitionInBegin() end
    function Menu:onTransitionInEnd()
    	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    end
    function Menu:onTransitionOutBegin()
    	self:removeAllListeners()
    end
    function Menu:onTransitionOutEnd() end
     
    -- change scene
    function Menu:gotoScene(xscene)
    	scenemanager:changeScene(
    		xscene, 1,
    		transitions[math.random(1, #transitions)],
    		easings[math.random(1, #easings)]
    	)
    end
    Hope this helps
    https://wiki.gideros.rocks/index.php/SceneManager
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • Thank you veeeery much @MoKaLux !!!
    You're making it true for me that Gideros Mobile community is the best!

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited April 2023
    Here is a project that uses scenemanager, you may have a look? (just download the zip and open the gideros project):
    https://github.com/mokalux/GIDEROS_PLATFORMER_CBUMP_GCAM_TECS
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited April 2023
    EDIT: I tested your code and it works fine but you need:
    - add scenemanager to gideros plugins (right click Plugins) -> add plugin


    - require "scenemanager"

    ;)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • edited April 2023
    Talking about your repository, I just tried now, but seems like there's something wrong. The menu scene appears with just a blue screen. But it's useful anyway. Thank you for sharing!

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • the blue screen is maybe this application:setBackgroundColor(0x227178) :)

    PS: don't add your button to the stage but add it to your scene (here the menu scene represented by self)
    function Menu:init()
    	local botao = Bitmap.new(Texture.new("button.jpg"))
    --	stage:addChild(botao) -- not so good
    	self:addChild(botao) -- prefered way, self is your menu sprite defined in Menu = Core.class(Sprite)
    end
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited April 2023

    Talking about your repository, I just tried now, but seems like there's something wrong. The menu scene appears with just a blue screen. But it's useful anyway. Thank you for sharing!

    Ah I see, in the repository the menu scene is empty, just blue background, I was focusing on cbump plugin example. All happen in the LevelX scene :)

    This example is more complete :p https://github.com/mokalux/PLATFORMER_CBUMP_TECS_20221101_wodt
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • MoKaLuxMoKaLux Member
    edited October 23
    hi fellow Giderians, after reading through this forum I noticed scenemanager may not be as "safe" as I thought (memory leaks?).

    I have read to prevent memory leaks you should initialise all your stuff in onTransitionInEnd, I tried this and other stuff but still some memory leaks happen.

    Now I want to move away from scenemanager but I have some doubts :s

    Is this how you would do a scene transition?
    	elseif k == 1 then
    --		scenemanager:changeScene("levelX", 1, transitions[1], easings[3]) -- please begone!
    		self:removeAllListeners()
    --		stage:removeChild(Menu) -- I don't know how to do it :-(
    		for i = stage:getNumChildren(), 1, -1 do
    			stage:removeChildAt(i)
    		end
    --		Menu = nil -- nope, I need to keep it
    		collectgarbage() -- seems to do its job :-)
    		stage:addChild(LevelX.new()) -- this is the next scene :-)
    	elseif k == 2 then --...
    How to make stage:removeChild(Menu) work? Is this the correct way?

    Thank you for your insights ;)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • piepie Member
    How to make stage:removeChild(Menu) work? Is this the correct way?


    I guess so, then you can say Menu = nil and collectgarbage() or wait for the next auto collection.. unless you need to keep it alive for some reason. Why do you want to keep it existing in the background?

    You could also add Menu directly to your Scene instead of adding it to stage.
    AFAIK what might cause leaks is something that keeps being created (initialized) and never removed. If you manage to add stuff as local or as a property of the scene itself it should be removed as soon as the scene itself is removed.

    There is also another thing that might happen: if you collectgarbage during a transition between two scenes, it might not be guaranteed that the first scene is already "dead" when you call the collector - therefore it cannot remove (yet) the stuff loaded in the first scene. I am not sure to remember when that occurred to me: I think I was reloading the same scene with different parameters (like from level1 to level2)
    When I had this issue I just added an almost empty "passage scene/loading screen" whose only reason to be was to allow cleaning up the stuff in the previous scene before loading the new one.

    Likes: MoKaLux

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