Jump to content

How to kill a sprite after going out of bounds


MarvinKome
 Share

Recommended Posts

I'm trying to kill a sprite after it passes the bounds in my game. I've tried using the phaser example https://phaser.io/examples/v2/sprites/out-of-bounds  this is my entire script, please help me

<script type="text/javascript">	
			var game = new Phaser.Game(1040, 450, Phaser.AUTO, '', { preload: preload, create: create, update: update });
			var platforms;
			var player;
			var bot;
			
			function preload(){
				game.load.image('sky','assets/rpg-bg.png');
				game.load.spritesheet('player','assets/player-walk.png', 87,125);
				game.load.spritesheet('player-attack','assets/player-shot.png', 113, 132);
				game.load.image('player-death','assets/player-death.png');
				game.load.spritesheet('bot','assets/bot.png', 112, 141);
			}
			
			function create(){
				game.physics.startSystem(Phaser.Physics.ARCADE);
				
				// Backgrounds
				game.add.sprite(0, 0, 'sky');
												
    			bot = game.add.sprite(1000, game.world.height, 'bot');
    			game.physics.arcade.enable(bot);
    			bot.body.gravity.y = 100;
    			bot.anchor.setTo(0.5, 0.5);
    			bot.body.collideWorldBounds = true;
    			bot.body.checkCollision.none = true;

    			bot.animations.add('run');
			    bot.animations.play('run', 10, true);
			    
			    bot.checkWorldBounds = true;
			    bot.events.onOutOfBounds.add(killBot, this);
				
				// Player
				player = game.add.sprite(32, game.world.height, 'player');
				game.physics.arcade.enable(player);
				player.body.bounce.y = 0.1;
				player.body.gravity.y = 100;
				player.body.collideWorldBounds = true;
				player.animations.add('run');
				player.animations.play('run', 10, true);
			}
			
			function update(){
				// Bot Movement
				bot.body.velocity.x = -150;
			}
			
			function killBot(bot){
				bot.kill();
			}

		</script>

Thank you

Link to comment
Share on other sites

try this way

<script type="text/javascript">	
			var game = new Phaser.Game(1040, 450, Phaser.AUTO, '', { preload: preload, create: create, update: update });
			var platforms;
			var player;
			var bot;
			
			function preload(){
				game.load.image('sky','assets/rpg-bg.png');
				game.load.spritesheet('player','assets/player-walk.png', 87,125);
				game.load.spritesheet('player-attack','assets/player-shot.png', 113, 132);
				game.load.image('player-death','assets/player-death.png');
				game.load.spritesheet('bot','assets/bot.png', 112, 141);
			}
			
			function create(){
				game.physics.startSystem(Phaser.Physics.ARCADE);
				
				// Backgrounds
				game.add.sprite(0, 0, 'sky');
												
    			bot = game.make.sprite(1000, game.world.height, 'bot'); //changed from game.add to game.make
    			game.physics.arcade.enable(bot);
    			bot.body.gravity.y = 100;
    			bot.anchor.setTo(0.5, 0.5);
    			bot.body.collideWorldBounds = true;
    			bot.body.checkCollision.none = true;

    			bot.animations.add('run');
			    bot.animations.play('run', 10, true);
			    
			    bot.checkWorldBounds = true;
			    bot.events.onOutOfBounds.add(killBot, this);
    			game.add(bot);                                             //added to game
				
				// Player
				player = game.add.sprite(32, game.world.height, 'player');
				game.physics.arcade.enable(player);
				player.body.bounce.y = 0.1;
				player.body.gravity.y = 100;
				player.body.collideWorldBounds = true;
				player.animations.add('run');
				player.animations.play('run', 10, true);
			}
			
			function update(){
				// Bot Movement
				bot.body.velocity.x = -150;
			}
			
			function killBot(bot){
				bot.parent.remove(bot);
			}

		</script>

 

Link to comment
Share on other sites

You didn't mention what isn't working - or what happens - when you posted the original code, but it's hard to help debug/fix without knowing the issue:) Also it will look the same whether you kill the bot when it goes out of screen or not IMO so IDK how you're monitoring whether it works or not, unless you add some debugging (console.log("KILLED") after bot.kill() would prob do)...

Link to comment
Share on other sites

You can add a platform at the bottom (its y is -10 now to show where it is, and you can remove debug rendering) of your game:

var game = new Phaser.Game(1040, 450, Phaser.AUTO, '', { preload: preload, create: create, update: update ,render:render});//you can remove render:render
var platforms;
var player;
var bot;

function preload(){
    game.load.image('sky','assets/rpg-bg.png');
    game.load.spritesheet('player','assets/player-walk.png', 87,125);
    game.load.spritesheet('player-attack','assets/player-shot.png', 113, 132);
    game.load.image('player-death','assets/player-death.png');
    game.load.spritesheet('bot','assets/bot.png', 112, 141);
}

function create(){
    game.physics.startSystem(Phaser.Physics.ARCADE);
    // Backgrounds
    platform=game.add.sprite(0,game.world.height-10 );
    game.physics.arcade.enable(platform);
    platform.body.setSize(game.width,100);
    platform.body.immovable=true;
    
    game.add.sprite(0, 0, 'sky');
                                
    bot = game.add.sprite(500, game.world.height-100, 'bot');
    game.physics.arcade.enable(bot);
    bot.body.gravity.y = 100;
    bot.anchor.setTo(0.5, 0.5);
    bot.animations.add('run');
    bot.animations.play('run', 10, true);
    
    bot.checkWorldBounds = true;
    bot.events.onOutOfBounds.add(killBot, this);
    
    // Player
    player = game.add.sprite(32, game.world.height-100, 'player');
    game.physics.arcade.enable(player);
    player.body.bounce.y = 0.1;
    player.body.gravity.y = 100;
    player.body.collideWorldBounds = true;
    player.animations.add('run');
    player.animations.play('run', 10, true);
}

function update(){
    // Bot Movement
    game.physics.arcade.collide(bot,platform);
    game.physics.arcade.collide(player,platform);
    bot.body.velocity.x = -150;
}

function render(){              //you can remove this
    game.debug.body(platform);  //you can remove this
}                               //you can remove this

function killBot(bot){
    bot.kill();
}


 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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