Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Layers images and sprites — Gideros Forum

Layers images and sprites

DikkesnoekDikkesnoek Member
edited January 2013 in General questions
Hello my Gideros friends.

I still do not understand how sprites and images are handled when they are placed sequentially.
In my test code I use a single stage. I first do some background stuff then the bodies and spites.
For every addition I use: stage:addChildAt(bitmap, x), So at the end I have 11 indexes (layers).
Some sprites with body are handled dynamicly. Problem is that a dynamic body (index 6) will be
updated in the onEnterFrame will always be placed on top of a image who has an index of 10.
Weird?

Thanks in advance,

Marc

Comments

  • zvardinzvardin Member
    edited January 2013
    If you use addChildAt and provide an index that is greater than the number of children then it will only be placed the last index of the table. So:
        local test = Sprite.new()
    local test2 = Sprite.new()
    local test3= Sprite.new()
    test.name = "test"
    test2.name = "test2"
    test3.name = "test3"
    stage:addChild(test)
    stage:addChild(test2,10)
     
    -- eventhough test2 was added at index 10, it's placed in the 2nd index
    for i = 1, stage:getNumChildren() do
    	print(stage:getChildAt(i).name)
    end
     
    --test3 will show up last 
    stage:addChild(test3,3)
    for i = 1, stage:getNumChildren() do
    	print(stage:getChildAt(i).name)
    end
  • DikkesnoekDikkesnoek Member
    edited January 2013
    I do something like:
    local background = Bitmap.new(Texture.new("Graphics/BG.png"))
    stage:addChildAt(eyesSprite, 1)
     
    local sprite1 = Bitmap.new(Texture.new("Graphics/Sprite1.png"))
    stage:addChildAt(sprite1, 2)
     
    -- Create some dynamic sprites and bodies on layer 3.
    for i=0, 10 do
    	-- Create bodie and fixtures.
    	-- Create distance joint.
    	-- Create rope joint.
     
    	testSprite = Bitmap.new(Texture.new("Graphics/test.png"))
    	-- etc.
     
    	stage:addChildAt(chainSprite, 3)
     
    	actors[testBody] = testSprite
    end
     
    local sprite4 = Bitmap.new(Texture.new("Graphics/Sprite4.png"))
    stage:addChildA(sprite, 4)
    The testSprites positions will be changed during the gameplay and
    will be updated in the onEnterFrame() What you see is that sprite4
    will be presented below the testSprites on layer 3. Am I right that
    you can add more than one sprites on one index (layer)?

    Regards,

    Marc
  • You can't add more than one sprite at the same index. You could wrap things in parent sprites to use layers though.
  • Thanks. Do you have an idea why the dynamic object (index 3) is always
    on top of sprite4?

    Regards,

    Marc
  • @Dikkesnoek - If the code above is the actual code that you are using, you have 'addChildA' rather than 'addChildAt' for sprite 4.
    But, as @zvardin says, you can't add multiple sprites at index 3, so I'm not sure what happens when you try to do that in your code.
  • @petec, when you try to add multiple sprites at the same index, they get moved, so the previous sprite at #3 now becomes #4 and the one you inserted is now #3.
    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
  • Thanks to all. @OZApps, do you mean that if I will add 10 sprites in my loop example at index 3, sprite4 (after the loop) will be placed on 3+10= index 13? I will recheck the code. Still difficult to get all graphical stuff in the right order.

    Marc
  • @Dikkesnoek,
    think of it as cards, if you place a card on top of the pile, the first card now moves down. repeat this a couple of times by adding a new card on the top of the pile, your top most card would now be at position 'n', n being the number of cards you added on top of the pile.

    Therefore to make things easier, you can also group the sprites into groups, so that the groups can then be moved to front or back with all the containing sprites. Almost like a layer with several sprites on them.
    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
  • I understand. But how can you reach a specific dynamic sprite in the onEnterFrame() handler. I create a lot of sprite+bodies in a loop and I have a separate main dynamic sprite. You don't know it's index. I want to change the main sprite body and image on the fly so I need to do a removeChildAt(?) and do a addChildAt(?).

    Regards,

    Marc
  • You can always use
    parent:getChildIndex(child) -- to get index
  • SinisterSoftSinisterSoft Maintainer
    edited January 2013
    Instead of creating the object/sprite, setup everything in a table with a depth variable. Sort the tabel (there is a lua command to do this) based on depth. Then fill in a list of sprites (create them or reuse them if they already exist) with the data from the table. Hide any previously created but unused sprites. At this stage also clip them to the REAL screen (not the scaled screen) so that you are not drawing something that physically not on screen (but might be on the logical screen). This way you can get them drawn in whatever order you like.

    Once you code it it's just 3 steps:

    1) the main game handling the table.

    2) the sort

    3) creating/remapping/hiding/clipping the on screen objects.

    Likes: Nascode

    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
    +1 -1 (+1 / -0 )Share on Facebook
  • @petec, when you try to add multiple sprites at the same index, they get moved, so the previous sprite at #3 now becomes #4 and the one you inserted is now #3.
    @OZApps - thanks for the explanation.
  • Thanks again. Big problem in my design is that I create a combination of more than 50 equal 4x2 pixel sprites to create a sort of elastic rope/chain. So when add more sprites, I don't know the exact index of the other sprites. These sprites need some special treatment during the game. I can say that I fount this quiet complicated to handle and cost me a lot of time these days.

    Best regards,

    Marc
Sign In or Register to comment.