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.
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:
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?
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.
@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
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.
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 ). 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
Comments
https://play.google.com/store/apps/developer?id=Into-It+Games
http://appstore.com/LidiaMaximova
Is there any example, about how to manage this?
Likes: MysiaGames
https://play.google.com/store/apps/developer?id=Into-It+Games
http://appstore.com/LidiaMaximova
https://play.google.com/store/apps/developer?id=Into-It+Games
http://appstore.com/LidiaMaximova
Each time they move, you just need to add it to another sprite in array.
Haven't test this yet.
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.
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
https://play.google.com/store/apps/developer?id=Into-It+Games
http://appstore.com/LidiaMaximova
I leave here my code for every frame:
https://deluxepixel.com