afcruzs Posted January 10, 2017 Share Posted January 10, 2017 Is there any way to add a listener that triggers when a sprite is visible by the camera?. The object Sprite contains inCamera, but it seems to re compute the bounds of the sprite according to this: http://www.html5gamedevs.com/topic/8936-does-phaser-handle-object-visibility-according-to-camera/, and that can be expensive. In my case I need to do this for many sprites on the screen, so I want to know if there is a more efficient way to do it. Link to comment Share on other sites More sharing options...
samme Posted January 11, 2017 Share Posted January 11, 2017 If `sprite.checkWorldBounds` is on then you can use `sprite.inCamera` without any additional cost. Otherwise you can check game.world.camera.view.intersectsRaw(sprite.left, sprite.right, sprite.top, sprite.bottom) Link to comment Share on other sites More sharing options...
afcruzs Posted January 11, 2017 Author Share Posted January 11, 2017 Thanks for your answer samme In the docs (https://phaser.io/docs/2.6.2/Phaser.Sprite.html#checkWorldBounds) says that checkWorldBounds calculates the bounds every time and that it can be a expensive operation. Is it worst to use inCamera withouth having set checkWorldBounds to true?. Using intersectsRaw has linear time cost with respect to the size of camera.view. I am not sure, but it seems like the 3 approaches end up doing calculations in linear time (maybe checkWorldBounds is faster?). I think that using a quadtree or a structure like that can make this operation much faster, but I don't know if the current API has something like that, of if that is even possible anyway. Link to comment Share on other sites More sharing options...
samme Posted January 12, 2017 Share Posted January 12, 2017 Either `checkWorldBounds` or `inCamera` triggers sprite.getBounds() so the cost of using one is probably similar to using both. But there's no reason to add `checkWorldBounds` if you don't need it. Try `intersectsRaw()` and see if it performs OK. Otherwise you can try `arcade.overlap()`, which takes advantage of sorted Groups. Link to comment Share on other sites More sharing options...
afcruzs Posted January 17, 2017 Author Share Posted January 17, 2017 Thanks samme. I'll try that. Link to comment Share on other sites More sharing options...
Recommended Posts