ismaPC95 Posted May 4, 2018 Share Posted May 4, 2018 npcplayerUno = this.physics.add.sprite(800, 450, 'enemy').setScale(0.2); npcplayerUno.setBounce(0.1); npcplayerUno.setCollideWorldBounds(true); this.physics.add.collider(player, npcplayerUno, killEnemy, null, this); //TRY TO DO AN IA //pasar otra variable por parametro que sea la del enemigo en cuestion, por si se realiza con varios if(npcplayerUno!=undefined){ if(player.body.position.x<npcplayerUno.body.position.x){ setTimeout(function(){/*npcplayerUno.body.position.x=-1;*/ npcplayerUno.setVelocityX(-95); npcplayerUno.anims.play('moveEnemyLeft', true); },500); }else if(player.body.position.x>npcplayerUno.body.position.x){ setTimeout(function(){/*npcplayerUno.body.position.x=+1;*/ npcplayerUno.setVelocityX(95); npcplayerUno.anims.play('moveEnemyRight', true); },500); }else{ npcplayerUno.setVelocityX(0); npcplayerUno.anims.stop(); } } function killEnemy(player, npcplayerUno){ if(npcplayerUno.body.touching.up && player.body.touching.down){ player.setVelocityY(-350); npcplayerUno.setVelocityX(0); npcplayerUno.scaleY=0.12; //npcplayerUno.destroy(); } } Link to comment Share on other sites More sharing options...
icp Posted May 4, 2018 Share Posted May 4, 2018 Stop using your sprite properties after you destroyed it in your update loop. Always check if it exists before updating it. ismaPC95 1 Link to comment Share on other sites More sharing options...
ismaPC95 Posted May 4, 2018 Author Share Posted May 4, 2018 How can I do that check? Because I have tried many things but I can not omit the mistake. Thanks for the quick reply. Link to comment Share on other sites More sharing options...
icp Posted May 5, 2018 Share Posted May 5, 2018 if (npcplayerUno.exists === true) { npcplayerUno.setVelocityX(95); // Do your stuff here. // If it doesn't have an exists property create your own and change it to false on destroy event. } Link to comment Share on other sites More sharing options...
Recommended Posts