Pooling Sprites/Physics Objects and changing properties?

Hi,

(i’ve not posted this in the GE forum because it can apply to just display objects too)

What would be the most efficient way to pool sprites so that items off screen are not accessed by the renderer etc?

eg say i have a pool of 100 reusable display objects which I can get from the pool 1 at a time… then when 3 connect in my game I remove them from the play area and add them back to the reusable pool. I can then use this display object again without instantiating (which should save on memory/gc/speed etc?), but I may want to change the object’s graphic (if it’s a sprite) and change the physics body assigned (if it’s in the Game Edition)

but aside from the GE stuff, when it’s not in use it should be inactive as possible but without removing the object (eg not rendered etc even though it’s offscreen)

do i just set it’s visibility to false? is there anything else I can do. eg store it in an array but remove it from the display lsit etc?

hope i’ve explained this clearly enough, obviously some examples would’ve helped!

thanks for any advice
regards
J

[import]uid: 6645 topic_id: 3452 reply_id: 303452[/import]

i was just looking at the multi-puck example. i’ve noticed the allDisks array is never modified when a puck is removed, presumably this is just an omission from the code? ie there’s nothing that would remove it automatically when calling removeSelf()? I know removeSelf does some behind-the-scenes work for safe removal of physics object so I was just wondering if it’s doing anything else with it’s reference that would affect items referencing it [import]uid: 6645 topic_id: 3452 reply_id: 10365[/import]

If you want an object not to have an effect on the render process, set it invisible. You need to do this even with objects that are not on the screen. I use this approach a lot. I create the maximun number of objects that I need from a certain type at a given time and store them inside a group/table. When I want to span one, I loop throught the table and and check which one is invisible, make it visible, change the properties and voila, job done.

For the Android platform this is evne more important as the Dalvik Garbage Collector will kick you in the rear if you create and remove objects constantly. [import]uid: 5712 topic_id: 3452 reply_id: 10384[/import]

what about the physics? should i remove the body when its offscreen (ie “destroyed” in my game, but still existent in the pool). Most likely i will want to change the sprite (eg a different character animation from the spritesheet, representing a new type of enemy) and hence the physics shape too, so I guess I’ll need to destroy the physics body and recreate … I’m just wondering the fastest and most memory efficient way of doing this. If I’m needing to change the sprite/animation, is pooling going to help? I wasn’t planning that i’d have a pool for each sprite type as I could end up having a lot created up front.

thanks
j [import]uid: 6645 topic_id: 3452 reply_id: 10387[/import]

Mike,

check my post here about performance issues on changing properties.
http://developer.anscamobile.com/forum/2010/11/13/performance-tip-setting-isvisiblefalse-slow-reading-display-object-xy-slow

Obviously I’m pushing it with 10,000 objects but it demonstrates the issue. setting isVisible = false for items out of stage bounds slows me down from 30fps to 15fps

Also however i’ve noticed with my optimized method that it runs well at 30fps, but after I’ve scrolled about 15 screens down it drops from 30fps to 15fps

(really i also need a better way of checking for onscreen items that doesn’t involve looping through all 10,000. once ive found one outside each of the top and bottom bounds, then I shouldnt need to process the rest as they are all stored in y order)

regards
j [import]uid: 6645 topic_id: 3452 reply_id: 11046[/import]