Jump to content

Resetting a group


Jcook894
 Share

Recommended Posts

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

`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();
}

 

Link to comment
Share on other sites

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

 Share

  • Recently Browsing   0 members

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