Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Passing self? - Gideros Forum

Passing self?

hugocostahugocosta Member
edited March 2013 in General questions
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!

Dislikes: yarnee

+1 -1 (+0 / -1 ) Share on Facebook

Comments

  • bowerandybowerandy Guru
    Accepted Answer
    @hugocosta, I don't completely understand what you are trying to do but...

    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
    +1 -1 (+2 / -0 ) Share on Facebook
  • @bowerandy, let me try to better explain my problem.

    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? :)
  • ar2rsawseenar2rsawseen Maintainer
    edited March 2013
    Oh you need to add
    scene1 = Core.class(Sprite)
    and
    loadscene = Core.class(Sprite)
    basically for each instance you will be using in the stage tree hierarchy

    That basically means that you are creating class which will inherit from Sprite and Sprite is a base element of the displayed tree hierarchy
  • @ar2rsawseen, I'm using that code, i've posted a very reduced version of my code. :)
  • ar2rsawseenar2rsawseen Maintainer
    edited March 2013
    Then what error does it give, when you use self?
  • @hugocosta, I'm sorry, I still have no idea what you are trying to accomplish. What is this loadscene.lua? And why don't you use the (scene) parameter in load scene:init()?

    It looks to me like (for whatever reason) that your scene1 object is never being added to the stage.

    best regards
  • @bowerandy

    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)

  • @ar2rsawseen

    The error is:

    loadScene.lua:153: attempt to index global 'self' (a nil value)
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Ok the you should do as @bowerandy said:
    3. scene1.lua
    function scene1:postInit()
        loadscene.new(self)
    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)
        scene:addChild(background)
    end

    Likes: hugocosta

    +1 -1 (+1 / -0 ) Share on Facebook
  • @ar2rsawseen and @bowerandy, Thank you!

    It's working now, the solution was as you have pointed out, use postInit() and pass "self" as a parameter.
Sign In or Register to comment.