MarvinKome Posted September 5, 2017 Share Posted September 5, 2017 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 More sharing options...
RollinSafary Posted September 6, 2017 Share Posted September 6, 2017 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 More sharing options...
MarvinKome Posted September 7, 2017 Author Share Posted September 7, 2017 Thanks Rollin but the debugger said game.add is not a function Link to comment Share on other sites More sharing options...
Taz Posted September 7, 2017 Share Posted September 7, 2017 game.make.sprite() is used to create the sprite w/out adding it to the world. Then later you can add it to the world manually like this: game.world.add(bot); // instead of game.add(bot); Link to comment Share on other sites More sharing options...
Taz Posted September 8, 2017 Share Posted September 8, 2017 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 More sharing options...
samid737 Posted September 10, 2017 Share Posted September 10, 2017 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 More sharing options...
Recommended Posts