Jump to content

How do I deactivate object when it is offscreen?


jagd
 Share

Recommended Posts

I'm creating a game with a very large world which will all be loaded from the beginning of the game.  However, to cut down on resource use, I want to make it so objects will only do stuff if they are in the player's view.  For example, an object that spawns a rockslide will only do so if it is within the player's view--or 'on-screen' if you want to use that term.

 

How do I do this?  Is there a way to say something like,

 

if(Object.isOnScreen){

    >>>do stuff

}

 

and would that work?

 

Or for example, if it is offscreen, delete it.

Link to comment
Share on other sites

Sprites have a onOutOfBounds function which will call a function if the sprite were to go out of the world's bounds.

sprite.events.onOutOfBounds(this.outOfBoundsFunction, this);outOfBoundsFunction: function() {    >> do stuff}

Not sure if that's what you're looking for though.

Link to comment
Share on other sites

  • 2 months later...

When I try that I get an error on my first line of code below: alien.events.onOutOfBounds is not a function.

alien.events.onOutOfBounds(this.outOfBoundsFunc, this);OutOfBoundsFunc = function() {      ... do stuff};

'alien' is the sprite, 'aliens' is the group, if I change the first line to 'aliens' I get the error: aliens.events is undefined

What am I doing wrong? 

Link to comment
Share on other sites

hmmm... maybe because this is a sprite in a group, it needs to be written differently, so it knows it's talking about the sprite in the group?

sprite.events.onOutOfBounds(this.outOfBoundsFunc, this);OutOfBoundsFunc = function() {      ... do stuff};

the sprite is 'alien' in a group called 'aliens'. How do I apply the above in this case? 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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