Jump to content

How to actually properly kill offscreen objects?


mraak
 Share

Recommended Posts

I know there is outOfBoundsKill property on Sprite, but what's the best way to apply it for scrolling background, so that element that move out of the picture can be killed and recycled later. Not that I haven't defined neither a world or a camera since I have infinite scrolling. 

 

So it's basically just a group called background, moving towards left forever. In it, I would like to automatically kill the elements that are no longer in the visible area of the screen. 

Link to comment
Share on other sites

You mean something like object pooling?

var lilyPads; // groupfunction create() {   lilyPads = game.add.group();   // create in a for loop, whatever   var lily = lilyPads.create(Math.random() * game.width, Math.random() * game.height, 'lilypad', 0);   lily.events.onOutOfBounds.add(lilyPadOOB, this);   lily.checkWorldBounds = true;}// sprite out of boundsfunction lilyPadOOB(lily) {   lily.kill();}// called when you need to spawn a recycled / new spritefunction spawnLilyPad() {   var lily = lilyPads.getFirstDead();   if (!lily) {      lily = lilyPads.create(0, 0, 'lilypad');      lily.events.onOutOfBounds.add(lilyPadOOB, this);      lily.checkWorldBounds = true;   }   lily.reset(x, y);   lily.animations.frame = 0;}
Link to comment
Share on other sites

I'm already doing something like that, but wanted to know what exactly is checkWorldBounds and onOutOfBounds, which are these coordinates? The visible canvas area, a camera, or a defined world size? Here's the description from the docs, but I don't what the world is in this context.

  

If true the Sprite checks if it is still within the world each frame, when it leaves the world it dispatches Sprite.events.onOutOfBounds and optionally kills the sprite (if Sprite.outOfBoundsKill is true). By default this is disabled because the Sprite has to calculate its bounds every frame to support it, and not all games need it. Enable it by setting the value to true.

 

Link to comment
Share on other sites

In my game this event gets fired when the sprite is off-screen. I think this depends on the size of your world ( default world-size = display size? ).

Otherwise, just check the source. You can get it at github.

 

Oh, and for endless scrolling (like in my game) I move the world-objects instead of the player. Works pretty well.

Link to comment
Share on other sites

I'd also be moving the world objects. Another problem is if the objects appears off-screen to the right and then rolls into the scene, wouldn't it then kill it instantly upon creation? Instead of going into the scene, exiting on the left and then killing it?

Link to comment
Share on other sites

Good question. I spawn my sprites outside the screen, haven't got any issues.

Here is Phaser's source for this feature (26551 ff is your part) :

    if (this.checkWorldBounds)    {        //  The Sprite is already out of the world bounds, so let's check to see if it has come back again        if (this._cache[5] === 1 && this.game.world.bounds.intersects(this._bounds))        {            this._cache[5] = 0;            this.events.onEnterBounds.dispatch(this);        }        else if (this._cache[5] === 0 && !this.game.world.bounds.intersects(this._bounds))        {            //  The Sprite WAS in the screen, but has now left.            this._cache[5] = 1;            this.events.onOutOfBounds.dispatch(this);            if (this.outOfBoundsKill)            {                this.kill();                return false;            }        }    }
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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