Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Priority sprites — Gideros Forum

Priority sprites

MysiaGamesMysiaGames Member
edited April 2013 in General questions
Is there any idea how to change priority of many sprites on screen?
I'm doing a isometric game, i can't find any useful command to change the index of sprite on Gideros. Maybe i need to remove a add again every frame, but it seems highly expensive.

Comments

  • amaximovamaximov Member
    edited April 2013
    In the docs under "Sprite", there is the following API call:
     Sprite:addChildAt(child, index)
    That could help you layer to your desire. Not sure if you need to first remove the Sprite object from its parent so beware of that. Try just that one line without removing it from its parent. Also, if you want to say move a Sprite object to the front within its parent, you could do:
     Sprite:addChildAt(child, Sprite:getNumChildren())
  • It seems that can't use any arbitrary value as index.
    Is there any example, about how to manage this?
  • The bottom most object(be it a Bitmap or another Sprite) within a sprite has index 1, the top most has index "Sprite:getNumChildren()." All other objects within the Sprite fall in between. Does that clarify?

    Likes: MysiaGames

    +1 -1 (+1 / -0 )Share on Facebook
  • ar2rsawseenar2rsawseen Maintainer
    The bottom most object(be it a Bitmap or another Sprite) within a sprite has index 1, the top most has index "Sprite:getNumChildren()." All other objects within the Sprite fall in between. Does that clarify?
    That should be exactly like you described. What problems are you having?

  • How about creating array of sprites, then add the characters & objects to them?
    Each time they move, you just need to add it to another sprite in array.

    Haven't test this yet.


  • @ar2sawseen : Yes, that's true. The problem is what can i do if all the sprites move along the screen. How to update his index value?

    I'm think something useful could be to do some sort like:

    stage:remove(sprite[i]) --remove my sprite from the stage
    stage:addChildAt(sprite[i], screenHeight - sprite[i]:getY() ) -- add again with priority acordding Y value

    ... but it can't.
  • http://www.giderosmobile.com/forum/discussion/2526/sorting-sprite-children-#Item_12

    Maybe this will help? If you keep a table of the sprites you can use the table.sort feature to sort them on whatever you like and then loop through them calling addChild to put them in the right order.

    Likes: MysiaGames

    +1 -1 (+1 / -0 )Share on Facebook
  • Alright quick question. Say I have 2 bitmaps as children of the same Sprite. Bitmap A has index 1 and Bitmap B has index 2 (using Sprite:getChildIndex(BitmapA or B)). If I do Sprite:addChildAt(BitmapB, 1) does this move Bitmap A up to index 2 effectively reversing the bitmap layering? What I noticed that after doing such an operation and printing the index of each bitmap within the sprite, both printed the same index! I can post some code if you believe this is a bug or error in my code unless by design this is meant to happen.
  • It works! thanks guys!, and this routine is really very fast!
    I leave here my code for every frame:
    	table.sort( aZombies, function(a,b)
    	   return a.sprite:getY() > b.sprite:getY()
    	end)
    	for i = 1,num_zombies do
    		self:removeChild(aZombies[i])
    		self:addChildAt(aZombies[i],#aZombies-i+1)
    	end
  • It might be even faster if you refill the whole data from an array as there is no memory to allocate/reallocate so thus also less garbage collection. Then hide any unused sprites or add any additional sprites.
    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
Sign In or Register to comment.