Jump to content

OutOfBounds and BackInBounds Event


Recommended Posts



I want to build a game with a endless world.

In the world there are a lot of GameObjects, so that i get into performance issues when there are too much of them especially, when they have physics too.


So I thought it would be better to kill all sprites that are not in range of the Player and revive them when they come back into range. I saw there is a OutOfBounds-Event wich i may can use.

So i added the following to my update-method:

this.game.world.setBounds(                player.body.x-(1000),                player.body.y-(1000),                2000, 2000, false, false, false, false);

This keeps the Worldbounds always 1000 Pixels around the Player without adding physic walls, that the objects collide with.


Now i can listen to the outOfBounds on every gameObject

...enemy.events.onOutOfBounds.add(onOutOfBounds, enemy);...onOutOfBounds = function() {    this.kill();};

Fine :) ... but here comes my Problem. How do i detect when the player turns around and the killed sprite comes back into the worldBounds?

An Event like "backInBounds" would be really nice.


Or is this maybe a completely wrong approach for my problem?

Link to comment
Share on other sites

I might be wrong but, i think that you can do something like this:

if(sprite.outOfBoundsKill && game.world.bounds.contains(sprite.x, sprite.y)) {    sprite.revive();}

The logic behind is to check if it was killed by outofbounds and if the bounds of the world (which is a Phaser.Rect) contains the position of the sprite.

If these 2 conditions are true, we revive him.


I am not sure if this is going to work but it seems like a valid solution.


As to your approach to the problem, i think it's valid but might not have the desired effects. Since you are killing everything that goes out of the screen, those gameobjects will not render AND they will not update as well, so they will basically stop at the moment they are outofbounds.

Link to comment
Share on other sites

Hi ctmartinez1992,


Thanks for that hint!

I came to a very similar solution. Currently i do the contains-Check with a self-calculated rectangle (based on the player position) to detecting the "OutOfRange" and "BackInRange". I dont want to change the world-bounds every time the player moves.


I try to build a online multiplayer game, so I dont need "OutOfRange"-Objects to be rendered or updated by the client, this is the job of my server.


This Problem is solved for me - Thanks again!

Link to comment
Share on other sites


  • Recently Browsing   0 members

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