Jump to content

'Kill' and 'Revive' sprites individually in a Group


3man7
 Share

Recommended Posts

Hello,

I'm wondering if someone could help me solve this problem I have with 'Groups' in Phaser.

So I have a group called 'enemy_wave' and it has 2 sprites attached to it: 'e_blue' and 'e_red'. Now, I want to 'kill' (sprite.kill();) 'e_blue' sprite and 'revive' (sprite.revive();) it later.

Everytime I try to do that it also kills 'e_red', no matter how I put it.

this.enemy_wave = game.add.group();
this.enemy_wave.enableBody = true;
this.enemy_wave.physicsBodyType = Phaser.Physics.ARCADE;
for (i = 0; i < system_var.length; i++) {
    for (j = 0; j < system_var[i].length; j++) {

        switch(system_var[i][j]){
            case 'b':
                var e_blue = this.enemy_wave.create(40 + 40 * j, 40 + 40 * i, 'enemies');
                this.enemy_wave.add(e_blue);
            break;

            case 'r':
                var e_red = this.enemy_wave.create(40 + 40 * j, 40 + 40 * i, 'enemies');
                this.enemy_wave.add(e_red);
            break;
        }

    }
}

 

This is how I 'kill' sprites:

this.enemy_wave.forEachAlive(function (e_blue) {
    e_blue.kill();
}, this);

And this is how I 'revive' sprites:

this.enemy_wave.forEachDead(function (e_blue) {
    e_blue.revive();
}, this);

So yeah it doesn't matter if I kill 'e_blue' or 'e_red' because either way it 'kills'/'revives' all sprites attached to this group.

How do I go about this? What seems wrong here?

~Thanks!

Link to comment
Share on other sites

If you want to kill one sprite, avoid forEachAlive and forEachDead. All you need is

e_blue.kill();
// or
e_red.kill();
// or
this.enemy_wave.getChildAt(0).kill();
// or
this.enemy_wave.getChildAt(1).kill();

The calls you're making are essentially kill ever living sprite and revive every dead sprite.

If you use forEachAlive or forEachDead, don't reuse e_blue (or any other existing reference).

Link to comment
Share on other sites

@samme

I actually have more than 2 sprites in the group. (but only 2 enemies: red+blue) So what I mean is that I have a loop that creates multiple sprites (e.g. 20 blue enemies and 20 red enemies). They are 'separated' to say by 2 chars and 2 variables: 'b' for blue and 'r' for red + 'e_blue' (variable) / 'e_red' (variable).

I also have a two-dimensional array that looks like this:

system_var = [
    'bbb00000bbb',
    '000rrrrr000',
    'bbb00000bbb',
];

Basically everytime it sees the letter 'b' it creates a blue enemy and adds it to the group / same goes to the char 'r' for the red enemy. (0 = empty)

for (i = 0; i < system_var.length; i++) {
    for (j = 0; j < system_var.length; j++) {

        switch(system_var[j]){
            case 'b':
                var e_blue = this.enemy_wave.create(40 + 40 * j, 40 + 40 * i, 'enemies');
                this.enemy_wave.add(e_blue);
            break;

            case 'r':
                var e_red = this.enemy_wave.create(40 + 40 * j, 40 + 40 * i, 'enemies');
                this.enemy_wave.add(e_red);
            break;
        }

    }
}

------------

I thought that I could control these sprites by just using the variables I've provided them with.
(e.g. there are 20 blue enemies that uses variable 'e_blue' and 20 red enemies that uses variable 'e_red')
(so whenever I want all the blue enemies to move right I say e_blue.x++; and it moves all the 20 sprites I've created)...

What is the correct way of doing this? Do I have to create a group for blue and one for red and then place them inside the 'mother' group (aka enemy_wave) ? Wouldn't that create a bigger 'mess'?

Link to comment
Share on other sites

The sprites created within the for loops are all exactly the same objects when you are adding them to the group:

e_blue = enemy_wave.create(40 + 40 * j, 40 + 40 * i, 'object');
e_red = enemy_wave.create(40 + 40 * j, 40 + 40 * i, 'object');

console.log(e_blue);//exact same object, with same properties and values
console.log(e_red); //exact same object, with same properties and values

When you use the enemy_wave.forEach and are meaning to refer to a blue enemy, you are still saying:

foreach object-which I will name e_blue within this function- in enemy_wave-->kill e_blue. Javascript will do as told and kill every e_blue in the enemy_wave group. As a result all your objects are killed in the process... very sad indeed.

You could add some (custom) property to distinguish red enemies from blue ones in enemy_wave:

var e_blue = enemy_wave.create(40 + 40 * j, 40 + 40 * i, 'object');
e_blue.tint=0x000fff;
e_blue.unitColor=0;
enemy_wave.add(e_blue);

var e_red = enemy_wave.create(40 + 40 * j, 40 + 40 * i, 'object');
e_red.tint=0xff0000;
e_red.unitColor=1;
enemy_wave.add(e_red);

and then say this:

    enemy_wave.forEachAlive(function (enemy) {
       if(enemy.unitColor==0){
        enemy.kill();
       }
      }, this);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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