michael-Bell Posted April 10, 2014 Share Posted April 10, 2014 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 More sharing options...
castiboy Posted April 11, 2014 Share Posted April 11, 2014 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 More sharing options...
rich Posted April 11, 2014 Share Posted April 11, 2014 Make the game worlds just a little bit wider, but don't let the camera scroll. Then they can shoot while 'off screen' Link to comment Share on other sites More sharing options...
valueerror Posted April 11, 2014 Share Posted April 11, 2014 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 timespanfireball.timer.add(2500, particleBurst, this, fireball,1);which creates smoke when the fireball "dies" Link to comment Share on other sites More sharing options...
Recommended Posts