Adding recycled object to the front display
I am reusing the
FlxSpriteobjects in the game. I made a pool class, which returns the available objects in good ol' Flixel way. My game requires the reused objects to appear at the top of the display list. However, they don't. How can I do it?
I already tried removing objects from
FlxStatewhen they die and adding again when they are spawned, but it didn't solve. I don't want to
sortthe whole group if possible.
I was pretty sure that this problem may have occured before, so I've searched with quite many keywords in both google and forum (displaylist, front, add, insert, above, index...). Sorry, if it is already asked.
Have you tried to
add()the sprite again?
Yes I do, remove and add, consequently in spawn function. I should emphasize that I don't use the
recycle()method of the FlxGroup. I have a customized pooling approach. Sprite's kill() function is called when it dies with
super.kill()included. Then I get the next object in the group with
getFirstAvailable(). And I just set its
existsto true and
But if you say that this approach works surely, then I should search the problem somewhere else. I have a single camera, I add the objects directly to the FlxState, not with some FlxGroups. I don't use any
FlxGmethods to manage the display list.
You also need to set
exists, which is what
My game requires the reused objects to appear at the top of the display list
Not sure if you meant displaying it over everything else or as the first object of the display array. If it's the latter, you can use
Thank you, using
state.insert(state.length, sprite)have solved the problem. It is strange, because I tried it yesterday already. Maybe deleting the contents of export folder has an impact?
No, that shouldn't make any difference.
Something I didn't consider:
add()doesn't always add members at the end, see in the API docs:
FlxGroup will try to replace a null member of the array first. Failing that, FlxGroup will add it to the end of the member array.
So, keeping to insert an item to the end will actually grow the members list large and sparse?