Quick Links: Gideros Home | Download Gideros | Developer Guide
SceneManager: go to another scene within a scene
  • Hello im new to Gideros and wanted to try things out.

    However, i don't get it to work that a can go to another scene within a scene. So not from main.lua but from let's say scene1, i want to call "sceneManager:changeScene(scenes[2])". But that get's me an error of course, because scene1 doesn't know of sceneManagers exsistance as it was created within main.lua.

    Below the code:

    main.lua
     
    -- Initialise scene manager and scenes
     
    -- Create a new scene manager
    local sceneManager = SceneManager.new({
    ["scnTitlescreen"] = scnTitlescreen,
    ["scnTown"] = scnTown,
    })
    -- adding it to the stage
    stage:addChild(sceneManager)
     
    -- setting the transition between scenes
    local transition = SceneManager.fade
     
    -- setting scenes table
    local scenes = {"scnTitlescreen", "scnTown"}
     
    -- set the first scene to go to
    sceneManager:changeScene(scenes[1])


    titleScreen.lua
    scnTitlescreen = Core.class(Sprite)
     
    function scnTitlescreen:init(t)
    if t then
    print("scnTitlescreen: ", t)
    end
     
    -- Title textfield
    local title = TextField.new(FONT_LARGE, "This is the title screen")
    title:setTextColor(YELLOW)
    title:setPosition(application:getLogicalWidth()/2 - title:getWidth()/2, 50)
    self:addChild(title)
     
    -- add button to go to next scene
    local btnStart = Button.new(Bitmap.new(Texture.new("gfx/buttons/btnStart.png")), Bitmap.new(Texture.new("gfx/buttons/btnStart_down.png")))
    btnStart:setPosition(application:getLogicalWidth()/2 - btnStart:getWidth()/2, 600)
     
    btnStart:addEventListener("click",
    function()
    sceneManager:changeScene(scenes[2]) -- this doesn't work!
    end)
    self:addChild(btnStart)
     
     
     
     
     
    -- Event listeners for the scene
    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
     
    function scnTitlescreen:onTransitionInBegin()
    print("scnTitlescreen - enter begin")
    end
     
    function scnTitlescreen:onTransitionInEnd()
    print("scnTitlescreen - enter end")
    end
     
    function scnTitlescreen:onTransitionOutBegin()
    print("scnTitlescreen - exit begin")
    end
     
    function scnTitlescreen:onTransitionOutEnd()
    print("scnTitlescreen - exit end")
    end


    The error message i get is:

    scenes/titleScreen.lua:22: attempt to index global 'sceneManager' (a nil value)
    stack traceback:
    scenes/titleScreen.lua:22: in function
    lib/button.lua:50: in function


    Thats pretty obvious because in titleScreen.lua there is no sceneManager. How do i get a reference to sceneManager within titleScreen.lua ?

    Thanks a lot
  • Don't use local variable in your main.lua
    Use global variables like below and everything should work fine.

    Note: I didn't test it but it should work fine:D
     
    -- Create a new scene manager
    sceneManager = SceneManager.new({
    ["scnTitlescreen"] = scnTitlescreen,
    ["scnTown"] = scnTown,
    })
    -- adding it to the stage
    stage:addChild(sceneManager)
     
    -- setting the transition between scenes
    transition = SceneManager.fade
     
    -- setting scenes table
    scenes = {"scnTitlescreen", "scnTown"}
     
    -- set the first scene to go to
    sceneManager:changeScene(scenes[1])
  • Thats what i've tried also, but then i get the following error:


    lib/scenemanager.lua:335: attempt to perform arithmetic on field 'duration' (a nil value)
    stack traceback:
    lib/scenemanager.lua:335: in function


    I'm using the scenemanager version v1.0.4
  • pie +1 -1
    Member
    Hi, take a look here for a comprehensive example:
    http://dev.appcodingeasy.com/Gideros-Mobile/Manage-Scenes-in-Gideros-Mobile

    you're missing the duration parameter (how much time would it take to switch scenes)
    try with:
    sceneManager:changeScene(scenes[1], 1, sceneManager.flipWithFade, easing.outBack)


    but first make sure you have easing.lua in your project tree :)
  • Ah thats what i've figured out too now.

    But why does "sceneManager:changeScene(scenes[1])" work in main.lua? Without any duration and so on.
  • My idea after tracing scenemanager.lua fastly:

    In function SceneManager:changeScene (starting line 266) in line 293 it is setting self.tweening property to "true"

    When first time changing the scene :
    So onenterframe function (starting line 318) on line 319 because self.tweening is true it is committing "Return" and not executing duration arithmetic operation which was giving the error on line 335 bypassing it.

    After on init it is setting this property to false, and on second scene already doing the arithmetic operation with a nil value and raising the error.

    Note: Maybe i am mistaking and it is just a wild guess:D
  • Ah yeah seems to be right.

    Thanks for the help :)
  • jdbcjdbc +1 -1
    Member
    I usually use almost the same main.lua in all my games. I show my own splash screen, wait one second and then show first scene.

    I only change screen orientation and the scene array of course.

    And yes, the following lua code works because sceneManager changes the scenes without animation and at the moment.
    sceneManager:changeScene(currentScene)
    main.lua
    1K
  • antixantix +1 -1
    Member
    @Rednib, With SceneManager you should always supply the duration, effect, and easing type. Failure to do so will result in errors. Also use the scenes name when using SceneManager, not [1], [2], etc.

    Here are your codes fixed to work. My comments are in CAPS so you can see what I changed and why.
    -- Initialise scene manager and scenes
     
    -- Create a new scene manager
    sceneManager = SceneManager.new({ -- MAKE IT GLOBAL SO IT CAN BE USED EVERYWHERE (BY ALL CLASS INSTANCES)
    ["scnTitlescreen"] = scnTitlescreen,
    ["scnTown"] = scnTown,
    })
    -- adding it to the stage
    stage:addChild(sceneManager)
     
    -- setting the transition between scenes
    local transition = SceneManager.fade
     
    -- THESE ARE NOT REQUIRED
    -- setting scenes table
    --local scenes = {"scnTitlescreen", "scnTown"}
     
    -- set the first scene to go to
    sceneManager:changeScene("scnTitlescreen", 1, SceneManager.fade, easing.linear)
    -- USE SCENE NAME AND ALWAYS PROVIDE PARAMETERS (DURATION, EFFECT, EASING)
    scnTitlescreen = Core.class(Sprite)
     
    function scnTitlescreen:init(t)
    if t then
    print("scnTitlescreen: ", t)
    end
     
    -- Title textfield
    local title = TextField.new(nil, "This is the title screen")
    title:setTextColor(0xffff00)
    title:setPosition(application:getLogicalWidth()/2 - title:getWidth()/2, 50)
    self:addChild(title)
     
    -- add button to go to next scene
    local btnStart = Button.new(Bitmap.new(Texture.new("gfx/buttons/btnStart.png")), Bitmap.new(Texture.new("gfx/buttons/btnStart_down.png")))
    btnStart:setPosition(application:getLogicalWidth()/2 - btnStart:getWidth()/2, 600)
     
    btnStart:addEventListener("click",
    function()
    sceneManager:changeScene("scnTown", 1, SceneManager.fade, easing.linear) -- THIS NOW WORKS :D
    -- USE SCENE NAME AND ALWAYS PROVIDE PARAMETERS (DURATION, EFFECT, EASING)
    end)
    self:addChild(btnStart)
     
    -- Event listeners for the scene
    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
     
    function scnTitlescreen:onTransitionInBegin()
    print("scnTitlescreen - enter begin")
    end
     
    function scnTitlescreen:onTransitionInEnd()
    print("scnTitlescreen - enter end")
    end
     
    function scnTitlescreen:onTransitionOutBegin()
    print("scnTitlescreen - exit begin")
    end
     
    function scnTitlescreen:onTransitionOutEnd()
    print("scnTitlescreen - exit end")
    end

    The SceneManager github repo has a pretty good example of how SceneManager works...

    There is also an old but still relevant article on Scenes by @ar2sawseen on his site also..

    that should all be enough to get you up and running, good luck :bz
    Check out my DevBlog, my GitHub, and my games Falling Animals | Breaky Wall | Exetor

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Login with Facebook Sign In with OpenID

In this Discussion

Top Posters