Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
removeChild error on exiting and entering scene — Gideros Forum

removeChild error on exiting and entering scene

somezombiegmsomezombiegm Member
edited March 2014 in General questions
Sorry for the long question, I appretiate you reading this.

I have a general problem which involves my game objects and the menu bar which is on top of the screen. I have some "bullet" bitmaps which can fly in every direction, but since every time the user shoots I add a new child to the stage, the bullets end on top of the menu, which I dont want.

So to fix this, on my update function (which is called with the Event.ENTER_FRAME event) every time I add a new bullet as a child, I remove the menu and immediately add it as a child again. However the problem with this is that if i go back to the start scene (yeah I'm using ar2rs Game Template project) and then return to my game it keeps working, but if I change scene and return to my game scene a 3rd time, the "remove and then add" solution throws an error; on my remove line the error says: "The suplied sprite must be a child of the caller". I remove and add the menu like a thousand times before, I dont know why when I enter the scene for the third time it throws me this error, please help.

The menu bitmap IS being added as a child, but acording to the error it isnt.

I can't show any code cause it's too large, and when I reduce it for testing it with only two bitmaps, the error doesn't happen any more :S.

It would be good to know why this happens so I could fix it, however, another way around it would be welcome. Like for example a bitmap function that puts an image on top or behind. That would be cool actually.

Anyway, that was the question...

Comments

  • OZAppsOZApps Guru
    Accepted Answer
    @somezombiegame,
    have you looked at addChildAt?

    A simple way would be to have 2 layers, the game layer and the menu layer, keep the menu layer onTop of the game layer and keep adding your bullets to the game layer, that way it will be current in the game layer and also below the menu layer.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • Havent heard of addChildAt or layers either, let me read something about it and see how to implement it.

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @somezombiegm
    With layers you can do something like that:
    Scene = Core.class(Sprite)
     
    function Scene:init()
        self.bulletLayer = Sprite.new()
        self:addChild(self.bulletLayer)
     
        self.menu = Sprite.new()
        self:addChild(self.menu)
    end
     
    function Scene:addBullet(bullet)
        -- now bullet will be added to bullet layer
        -- and will always be under menu
        -- because it was added to the scene before menu
        self.bulletLayer:addChild(bullet)
    end
  • SinisterSoftSinisterSoft Maintainer
    Accepted Answer
    Do something like bullets=Sprite.new(), then stage:addChild(bullets), than add your bullets as child sprites to bullets.

    You can then loop though bullets and process them as you want (technically it might be faster to process them in a table then move specific table data to the correct bullet sprite though - this depends on what you are doing).

    Then add a new sprite called ui, eg ui=Sprite.new(), stage:addChild(ui). And then add your bits for your panel.

    Because ui was added to stage AFTER bulllets then it will normally always be on top of bullets on screen. You can add and remove sprites to bullets and they will always be below ui (unless you change the order of things on the stage).
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • Argh... @ar2rsawseen - faster than me again! :)
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • hi all,
    could the removeChild will auto release the sub group objects?
    regards
  • Thanks a lot for your help. I did used Sprites on a tutorial but I didn't knew you can implement them as layers. Having all my game elements on a Sprite and all my menu elements on another works fine. I just have to put stage:addChild(gameLayer) before stage:addChild(menuLayer). And all the corresponding elements to the corresponding layers as all of you said.

    @whidbey yeah, if I put stage:removeChild(gameLayer) all of the elements added to that layer will be gone.

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • @somezombiegm ,ok, and do you have a menu classes? would you share with me?thank you
  • somezombiegmsomezombiegm Member
    edited March 2014
    @whidbey Menu Classes? I'm using ar2rs Game Template which has a scene manager.

    http://giderosmobile.com/forum/discussion/480/gideros-game-template/p1

    Not sure if that's what you want.
  • Just one small point of concern, I am not sure who had started that thread. While removeChild will remove the children, there could be instances where the listeners still remain and in some bizarre cases cause funny things to happen. So while you can rely on the removeChild to remove all sub groups, etc make sure that you remove the eventListeners (if any) on the sub groups and grandchildren.

    Likes: somezombiegm

    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
    +1 -1 (+1 / -0 )Share on Facebook
  • Hello!
    I1m having a problem that: The supplied Sprite must be a child of the caller. I still didn't understand the structure of that 'cause every time is a different behaviour. Doesn't makes sense we add a sprite and not be able to remove that gives this error... doesn't makes any sense... I'll show an example
    local sprite = Sprite.new()
     
    self:addChild(sprite) -- works
     
    self:removeChild(sprite) --ERROR The supplied Sprite must be a child of the caller
    Can anybody explain this?
  • So long as "sprite" is still in scope, that code looks fine. Although I tend to use sprite:removeFromParent() instead, because I'm lazy. :)

    Likes: Paulo777

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • Paulo777Paulo777 Member
    edited March 2018
    @totebo haha this also have worked for me! But I mean in my case it gives me error because in my "sprite" I have 3 movieclips, maybe, I suppose to... Still in this scenario, removeFromParent seems to work but I noticed it remain in memory... Suppose that I want not my sprite to be in the stage, or child sprite, what Can I do to "destroy" it definetly?
  • Likes: Paulo777

    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
    +1 -1 (+1 / -0 )Share on Facebook
  • totebototebo Member
    edited March 2018
    what Can I do to "destroy" it
    Once there are no references to a sprite it is garbage collected automatically. So just set all the variables containing a reference to it to nil and you should be good.

    You can control when to garbage collect by using collectgarbage() like @apollo14 suggests, but this will only garbage collect sprites with no references. So long as there is one or more references to the sprite, it will never be garbage collected.

    Likes: Paulo777

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.