Jump to content

Performance - deactivate sprites outside the canvas drawing area


beakartJs
 Share

Recommended Posts

Hi!
I'm working on a platform game and I have a question.

If I have a map of sprites-objects in the game outside the field of view, should I deactivate these objects and activate them when they are seen? If so, what is the best, optimum way to deactivate sprites outside the drawing area? sprite.alpha = 0; and sprite.animations.stop (); ? Or other idea?

I am trying (in the loop forEach for sprite vs player) something like this:

 

// [...] main game loop   

    // loop for sprite example
    exampleSprites.forEach(function(sprite){  
         if(player.position.x>sprite.position.x-game.width/1.5 &&
              player.position.x<sprite.position.x+game.width/1.5 &&
              player.position.y>sprite.position.y-game.height/1.5 &&
              player.position.y<sprite.position.y+game.height/1.5) {
              if(!sprite.alpha) {
                  sprite.alpha=1; 
                  sprite.animations.play('run');
              }
          } else {
             if(sprite.alpha){
                sprite.alpha=0;
                sprite.animations.stop();
              }
          }
      }, this, true);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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