Jump to content

Deleting off camera group items


michael-Bell
 Share

Recommended Posts

I'm still figuring out Phaser, and I've made a mario clone that involves shooting bullets at enemies. I'm trying to destroy the bullets after they leave the camera view, but I'm having some troubles. I have tried using

 

        bullet = bullets.create(player.x, player.y+8, 'bullet');
        bullet.events.onOutOfBounds.add( goodbye, this );

 function goodbye(obj) {
 console.log(obj);
   obj.kill();
}

 

Howevery, this never fires, even after the bullet has left the camera. I think that it might be because my world bounds are much larger than the camera is, so I tried making a function to destroy the bullets after they leave the camera like this:

 

function checkBullet(){
bullets.forEach(function(item) {
    if(item.inCamera===false){
   item.kill()

   } });}

 

However this kills the bullets immediatly, which I also don't want. Is there a better way that I can go about this that will actually work?

Link to comment
Share on other sites

The invaders example shows a nice way of managing bullets.

I just adapted it for an "Asteroids"-like game I'm making it. I had to add one line though (otherwise it would run out of bullets, not being killed I believe.)

    bullets.setAll('checkWorldBounds', true); // <- extra line to make it work    bullets.setAll('outOfBoundsKill', true);

I'm running into a different problem with this code though... I want to "shoot" elements from outside the screen, and I want them killed when they exit the screen... this method kills them at creation.

Link to comment
Share on other sites

in my game bullets fly through the camera view ..  i create them at game.camera.x + game.camera.width      (at the right side of the screen.. you could add a few pixels to create them outside of the right bounds

 

and then i kill them when they leave the camera view on the left side with

 if (bullet.body.x < game.camera.x){bullet.kill();}

i know this is a very easy to handle setup...

 

otherwise you could stop killing them in combination with bounds and give them a lifespan instead..  

 

thats what i do with my fireballs:

fireball.lifespan=2500;

and then i start a timer with the same timespan

fireball.timer.add(2500, particleBurst, this, fireball,1);

which creates smoke when the fireball "dies" :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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