ye0jun Posted April 20, 2017 Share Posted April 20, 2017 I made virtual camera with container and I want show sprites only in this camera's range so I use 'removeChild' for out of camera's range sprites and again 'addChild' when they in camera. but 'removeChild' is very slow... then how to do it?? Quote Link to comment Share on other sites More sharing options...
Taz Posted April 20, 2017 Share Posted April 20, 2017 If you can group the sprites into containers, one per area for instance, you can reduce the amount of add/remove calls that way. Also, have you tried setting sprite.visible to true/false instead of adding/removing from stage? Quote Link to comment Share on other sites More sharing options...
ye0jun Posted April 20, 2017 Author Share Posted April 20, 2017 @Taz yes I tried visible to true/false but it just show/hide not influence Quote Link to comment Share on other sites More sharing options...
xerver Posted April 20, 2017 Share Posted April 20, 2017 11 hours ago, ye0jun said: @Taz yes I tried visible to true/false but it just show/hide not influence Can you explain what you mean by this? Quote Link to comment Share on other sites More sharing options...
Taz Posted April 20, 2017 Share Posted April 20, 2017 If you want to avoid updating and drawing sprites that go off-camera, setting the sprite's visibility can achieve this. There's still some extra processing to loop through and call render and check this.visible for each sprite, but the invisible sprites' transforms aren't updated and they aren't drawn either. So this can sometimes perform better than adding to and removing from the stage, for instance if the sprites are going on and off camera quickly and repeatedly, back and forth. On the other hand, sometimes adding to and removing from the stage can perform better, for instance if the sprites are moving on and off camera infrequently. You can also try combining both methods, for example when the sprite first goes off camera set sprite.visible to false. Then if it comes back into the camera's view soon, just set sprite.visible back to true. But if it stays off camera too long, then remove it from the stage. And grouping the sprites into containers can sometimes boost performance too. For instance, if several sprites always move on and off camera at about the same time, then you can reduce the amount of add/remove processing by adding/removing the whole container at once, instead of performing a separate add/remove for each sprite. And if a container is invisible, then its children aren't processed, so grouping can reduce the amount of invisible elements that need to be processed/checked. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.