Adding recycled object to the front display

  • I am reusing the FlxSprite objects 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 FlxState when they die and adding again when they are spawned, but it didn't solve. I don't want to sort the 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.

  • administrators

    Have you tried to remove() and 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 withgetFirstAvailable(). And I just set its exists to true and remove + add.

    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 alive to true along with exists, which is what revive() function does.

    You said:

    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 group.insert(0, object)

  • 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?

  • administrators

    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?

Log in to reply

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.