Jcook894 Posted December 16, 2016 Share Posted December 16, 2016 Hey everyone! first time poster here and Im kinda stuck on my game im coding. Its a basic side scrolling shooter that has a group of aliens that spawn randomly across the canvas. Im working on a restart function for when the player lives reach 0 or you kill all aliens. Ive gotten so far as to reset everything besides the aliens. Ive tried following this example https://phaser.io/examples/v2/games/invaders but it doesnt seem to work either. and my repo if anyone would like to try it out : https://github.com/Jcook894/Side_Scroller_game is there an easier way to reset this group? ive used the following code: //Creates a group of 35 aliens. function createAliens(){ aliens = game.add.group(); aliens.enableBody = true; aliens.createMultiple(35, 'aliens', 0, false); game.time.events.repeat(Phaser.Timer.SECOND, 20, resurrect, this); //ressurects the alien from the group and adds it to the group, and gives it movement & physics. function resurrect() { var ufos = aliens.getFirstDead(); if(ufos){ ufos.reset(game.world.randomX, game.world.randomY); ufos.body.velocity.setTo(10 + Math.random() * 40, 10 + Math.random() * 40); ufos.body.bounce.setTo(0.5, 0.5); ufos.body.collideWorldBounds = true; ufos.frame = game.rnd.integerInRange(0,36); } } } and my restart function function restart(){ lives.callAll('revive'); aliens.removeAll(); player.revive(); aliens.callAll('revive'); deadTxt.visible = false; score = 0; winTxt.visible = false; } Link to comment Share on other sites More sharing options...
samme Posted December 16, 2016 Share Posted December 16, 2016 `aliens.removeAll()` will empty the group, I don't think you want that. I think you can kill any remaining aliens and then restart the resurrection timer. var aliens, deadTxt, lives, player, score, winTxt; // ?? function start() { createAliens(); startTimer(); } function createAliens() { aliens = game.add.group(); aliens.enableBody = true; aliens.createMultiple(35, 'aliens', 0, false); } function startTimer() { game.time.events.repeat(Phaser.Timer.SECOND, 20, resurrect, this); } function resurrect() { var ufos = aliens.getFirstDead(); if (ufos) { ufos.reset(game.world.randomX, game.world.randomY); ufos.body.velocity.setTo(10 + Math.random() * 40, 10 + Math.random() * 40); ufos.body.bounce.setTo(0.5, 0.5); ufos.body.collideWorldBounds = true; ufos.frame = game.rnd.integerInRange(0, 36); } } function restart() { aliens.callAll('kill'); lives.callAll('revive'); player.revive(); deadTxt.visible = false; score = 0; winTxt.visible = false; startTimer(); } Jcook894 and drhayes 2 Link to comment Share on other sites More sharing options...
Jcook894 Posted December 16, 2016 Author Share Posted December 16, 2016 Thank you! that totally worked! Someone told me I should try to start my game state again and that fixed it, but got rid of another group on the canvas. Link to comment Share on other sites More sharing options...
Jcook894 Posted December 16, 2016 Author Share Posted December 16, 2016 14 hours ago, samme said: `aliens.removeAll()` will empty the group, I don't think you want that. I think you can kill any remaining aliens and then restart the resurrection timer. var aliens, deadTxt, lives, player, score, winTxt; // ?? function start() { createAliens(); startTimer(); } function createAliens() { aliens = game.add.group(); aliens.enableBody = true; aliens.createMultiple(35, 'aliens', 0, false); } function startTimer() { game.time.events.repeat(Phaser.Timer.SECOND, 20, resurrect, this); } function resurrect() { var ufos = aliens.getFirstDead(); if (ufos) { ufos.reset(game.world.randomX, game.world.randomY); ufos.body.velocity.setTo(10 + Math.random() * 40, 10 + Math.random() * 40); ufos.body.bounce.setTo(0.5, 0.5); ufos.body.collideWorldBounds = true; ufos.frame = game.rnd.integerInRange(0, 36); } } function restart() { aliens.callAll('kill'); lives.callAll('revive'); player.revive(); deadTxt.visible = false; score = 0; winTxt.visible = false; startTimer(); } Actually, ive tried this and its saying that startTimer is not a function. Am i supposed to call this outside of the gamestate? Link to comment Share on other sites More sharing options...
samme Posted December 17, 2016 Share Posted December 17, 2016 I would move all your helper functions outside `gameState` (sorry for reformatting): https://github.com/Jcook894/Side_Scroller_game/commit/eca49f70876dc247f97f50432fdff217be36afb5 Link to comment Share on other sites More sharing options...
Jcook894 Posted December 17, 2016 Author Share Posted December 17, 2016 2 hours ago, samme said: I would move all your helper functions outside `gameState` (sorry for reformatting): https://github.com/Jcook894/Side_Scroller_game/commit/eca49f70876dc247f97f50432fdff217be36afb5 Thanks again! I was calling all the helper functions in the update function and it was generating way to many aliens! Link to comment Share on other sites More sharing options...
Recommended Posts