erich Posted September 12, 2016 Share Posted September 12, 2016 Hi I have a script that I wish to implement so that if the player hits an enemy 3 times the game restarts, but it constally loops when touching, where am I going wrong ?? create function this.currentHits = 3; this.collisionHasOccurred = false; update function : this.game.physics.arcade.overlap(this.player, this.enemies, this.youLose, null, this); youlose function : youLose : function(player, enemies){ if (!this.collisionHasOccurred) { this.currentHits --; this.collisionHasOccurred = true; } else { this.collisionHasOccurred = false; } console.log(this.currentHits); console.log(this.collisionHasOccurred); if (this.currentHits === 0) { //this.game.state.start('Game'); console.log('GAME OVER'); } }, thanks in advance eric Link to comment Share on other sites More sharing options...
erich Posted September 13, 2016 Author Share Posted September 13, 2016 anyone ?? Link to comment Share on other sites More sharing options...
carlosnufe Posted September 13, 2016 Share Posted September 13, 2016 I did not understand the flag 'this.collisionHasOcurred'. When your youLose() method is called you should implement some time of delay because update is calling your method too fast and even though you put this flag the code is executed faster than you know. Try to deactivate the enemy for a while. erich 1 Link to comment Share on other sites More sharing options...
Milton Posted September 13, 2016 Share Posted September 13, 2016 I would rename collissionHasOccurred to overlapping. if (!this.game.physics.arcade.overlap(this.player, this.enemies, this.youLose, null, this)) overlapping = false; And then do not reset overlapping inside the handler (remove the else block), otherwise it will loop. erich 1 Link to comment Share on other sites More sharing options...
samme Posted September 14, 2016 Share Posted September 14, 2016 @erich You have the right idea but for any overlap longer than 2 frames (very likely) `collisionHasOccurred` will be flipping back and forth and the player will still accrue hits. You can use a timer: // @create: this.currentHits = 3; this.setPlayerCanCollide(); // @update: if (this.playerCanCollide) { this.game.physics.arcade.overlap(this.player, this.enemies, this.youLose, null, this); } // … setPlayerCanCollide: function() { this.playerCanCollide = true; } youLose: function(player, enemies) { this.currentHits--; this.playerCanCollide = false; this.time.events.add(1000, this.setPlayerCanCollide, this); // … } erich 1 Link to comment Share on other sites More sharing options...
erich Posted September 14, 2016 Author Share Posted September 14, 2016 brilliant ! thanks guys greatly appreciated Link to comment Share on other sites More sharing options...
carlosnufe Posted September 14, 2016 Share Posted September 14, 2016 3 hours ago, erich said: brilliant ! thanks guys greatly appreciated You are welcome. erich 1 Link to comment Share on other sites More sharing options...
Recommended Posts