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 <scenes/titleScreen.lua:21>
lib/button.lua:50: in function <lib/button.lua:46> |
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
Comments
Use global variables like below and everything should work fine.
Note: I didn't test it but it should work fine:D
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:
But why does "sceneManager:changeScene(scenes[1])" work in main.lua? Without any duration and so on.
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
Thanks for the help
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.
Here are your codes fixed to work. My comments are in CAPS so you can see what I changed and why.
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