Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
addChildAt constraints [solved] - Gideros Forum

addChildAt constraints [solved]

flashjaysanflashjaysan Member
edited July 22 in General questions
When I messed with addChild and addChildAt methods, I discovered that I couldn't pass an index that was beyond the number of already added child.
For example, if I added 2 images using addChild method, I couldn't then add a new sprite with addChildAt method and passing 4 as an index.
So my guess is if I want to use the addChildMethod, I can add a new sprite at the end of some kind of list of sprites using a number_of_childs + 1 index value. And if I want to replace a child at a specific index with a new one, I just have to pass the index of the original sprite.
In that case, does all the sprites with index superior or equal to that value are displaced with their index incremented by 1?

Comments

  • olegoleg Member
    edited July 22


    For example, if I added 2 images using addChild method, I couldn't then add a new sprite with addChildAt method and passing 4 as an index.
    So

    --addChild  -adds to the end of the array
     
    --the same thing:
    a = Sprite:getNumChildren()
    Sprite:addChildAt(child, a+1)

    ps.\\ to work with layers
    layer={}
    for i =1,100 do
       layer[i]=Sprite.new()
        stage:addChild(layer[i])  
    end
     
    layer[4]:addChild(sprite1)
     
    layer[17]:addChild(sprite2)
     
    ...
  • @oleg you cannot use addChildAt with no index. I tried that.
  • Anyway, do you have any answer to my question? The insertion one.
  • olegoleg Member
    edited July 22
    --stage{child1,child2,child3}

    stage:addChildAt(child1, 3)

    --stage{child2,child3,child1}

    stage:addChild(child4)

    --stage{child2,child3,child1,child4}

    stage:removeChildAt(2)
    --stage{child2,child1,child4}
  • flashjaysanflashjaysan Member
    edited July 22
    @oleg Wow! I'm not sure if it's correct. Let me take your code :
    --stage is empty
    child1 = Sprite.new()
    child2 = Sprite.new()
    child3 = Sprite.new()
    child4 = Sprite.new()
     
    stage.addChild(child1)
    stage.addChild(child2)
    stage.addChild(child3)
    --child1 is at index 1 now
    --child2 is at index 2 now
    --child3 is at index 3 now
     
    stage:addChildAt(child1, 3)
    --removes child1 at index 1 and places it at index 3 therefore pushing child3 (and everything next to it if it occurred) to one index higher
    --since index 1 is empty, shifts everything one index less than there current value
    --child1 is at index 3 now
    --child2 is at index 1 now
    --child3 is at index 2 now
     
    stage:addChild(child4)
    --appends child4 at the end of the list
    --child1 is at index 3 now
    --child2 is at index 1 now
    --child3 is at index 2 now
    --child4 is at index 4 now
    Is it like that?
  • keszeghkeszegh Member
    Accepted Answer
    i think by now you got it, in any case some answers:

    "In that case, does all the sprites with index superior or equal to that value are displaced with their index incremented by 1?"
    - yes

    "using a number_of_childs + 1 index value."
    - yes, use Sprite:getNumChildren()
Sign In or Register to comment.