Jump to content

Need a little help culling my asteroids


Uhfgood
 Share

Recommended Posts

Don't know if culling is the proper term, but essentially when the asteroids are off the screen completely, I would like to essentially be able to disable them, not really sure wht to do, I guess here's a body enable/disable -- and the visibility flag on sprites --

 

Basically i do wraparound by creating 4 sprites, one primary and 3 "cloned" sprites -- the 3 clones need only collisions to be handled when they're completely or partially on screen.  I still need physics on the primary asteroids because they move around and stuff, the clones only are updated based on position.

 

So I guess I'm just asking is messing with the body and visibility flags the thing I should do on the clones when they're off screen, or should I disable them some other way?

Link to comment
Share on other sites

You have sprite.events.onOutOfBounds and sprite.events.onEnterBounds (these events and more are rather hidden away sadly) if you want to disable the body, though a cleaner way would be to simply skip over sprites that have visible set to false in your collision processCallback:

game.physics.arcade.collide(player, asteroids, function(player, asteroid) {  // your collision code here}, function(player, asteroid) {  // if this function returns true, the separation routine is run and the collision happens - so we can just return the visible property, which will only run collisions on visible asteroids  return asteroid.visible;});
Link to comment
Share on other sites

Setting sprite.autoCull = true will stop rendering sprites outside the camera, though the tradeoff is their bounds must be checked. A faster way would probably be to just check each frame if the sprite's x and y coordinates are outside of the screen, and if so, set sprite.visible to false.

function update() {  group.forEach(function(sprite) {    if (sprite.x - sprite.width < 0 || sprite.x + sprite.width > game.world.width || sprite.y - sprite.height < 0 || sprite.y + sprite.height > game.world.height) {      sprite.visible = false;    }    else {      sprite.visible = true;    }  });}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...