Hi,
i'm trying out gideros mobile and it looks very good.
But i'm having a problem.
I'm using scenemanager to organize my game. (menu, scene1, scene2, scene3)
My scenes are very similar, and i'm building them dynamically:
scene1.lua:
scene1 = Core.class(Sprite)
function scene1:init()
print("Scene 1")
loadscene.new("scene1")
end
The problem is that in loadscene.lua i can't add the sprites to self, to make it work i have to add them to the stage.
And this prevents me to change the scene using scenemanager:changeScene (to go back to the menu for example), the sprites aren't unloaded.
Is there a way to access "scene1 self" inside the new loadscene object?
Thank you!
Comments
During the init() method an object is not fully constructed so there are some things you can't do. Try changing your code to use the method postInit() rather than init() and see if it does the job.
If this does work then take a look at this thread to see why postInit() was introduced:
http://www.giderosmobile.com/forum/discussion/2506/is-it-possible-to-call-subclasss-overwrited-function-in-baseclasss-init-function/p1
best regards
Likes: hugocosta, hgvyas123
Btw, i tried postInit(), same result.
Simplifying let's assume i have 4 files:
--------------------------------------
1. main.lua
sceneManager = SceneManager.new({
["menu"] = menu,
["scene1"] = scene1
})
stage:addChild(sceneManager)
sceneManager:changeScene("menu", 1, transition, easing.outBack)
---------------------------------------
2. menu.lua
[...]
function buttonClicked(self, event)
if self:hitTestPoint(event.x, event.y) then
sceneManager:changeScene("scene1", 1, transition, easing.outBack)
event:stopPropagation()
end
end
----------------------------------------
3. scene1.lua
function scene1:postInit()
loadscene.new("scene1")
end
----------------------------------------
4. loadscene.lua
[...]
function loadscene:init(scene)
[...]
local background = Bitmap.new(sceneBg)
background:setAnchorPoint(0.5, 0.5)
local bgX = application:getLogicalHeight()/2
local bgY = application:getLogicalWidth()/2
background:setX(bgX)
background:setY(bgY)
stage:addChild(background)
end
local function onMouseUpBackButton(self, event)
if self:hitTestPoint(event.x, event.y) then
sceneManager:changeScene("menu", 1, transition, easing.outBack)
event:stopPropagation() --stop all the touch events or mouse event
end
end
----------------------------------------
As you can see i have to use stage:addChild(background) to add the background, because if i use "self" it gives an error. And because i'm using "stage" when i call onMouseUpBackButton the background (and the scene) doesn't disappear.
Is it more clear now?
That basically means that you are creating class which will inherit from Sprite and Sprite is a base element of the displayed tree hierarchy
It looks to me like (for whatever reason) that your scene1 object is never being added to the stage.
best regards
loadscene.lua will construct my scenes based on the scene parameter that is passed on from scene1.lua, scene2.lua, scene3.lua, etc. (this scene parameter is being used in my code)
My game works perfectly, from the menu i can call any scene that i want. The selected scene loads all the sprites and this is all done by loadscene.lua. The problem is when i try to return from one of the scenes back to the menu via scenemanager:changescene
As i understand all the sprites must be added to self (the current scene) so that scenemanager can removed them all when changing scenes.
My question is why can't i use self:addChild(background) instead of stage:addChild(background)
The error is:
loadScene.lua:153: attempt to index global 'self' (a nil value)
3. scene1.lua
Likes: hugocosta
It's working now, the solution was as you have pointed out, use postInit() and pass "self" as a parameter.