Jump to content

Collision with group only collides onces


Recommended Posts

I'm having an issue which I'm unsure what's happening and can't find anyone else having a similar issue.


Basically, I create a group, create them, add a tween to animate them across the page. I create a player sprite and whenever the player collides with the other sprites in the group kill them and render another one. however, this only works on the first item I create.


    var group = game.add.group();
        group.enableBody = true;
        group.physicsBodyType = Phaser.Physics.ARCADE;


So create my group and then do a `for loop` to create all my enemies

    for (var i = 0i < 2i++) {
      c = group.create(game.width, 20'seacreatures''octopus0000');
      c.name = "enemy" + i;
      c.body.immovable = true;
      c.alive = true;
      c.alignIn(laneArr[Math.floor(Math.random() * laneArr.length)], Phaser.RIGHT_CENTER);
      game.add.tween(c).to({ x: -200 }, speedArr[Math.floor(Math.random() * speedArr.length)], Phaser.Quadratictrue00false);
    group.callAll('animations.add''animations''swim'Phaser.Animation.generateFrameNames('purpleFish'032''4), 30truefalse);


Below this I also render the player

    currPlayer = game.add.sprite(50game.height / 4 * 2 + 45"playerFish");
    currPlayer.body.immovable = true;
    currPlayer.physicsBodyType = Phaser.Physics.ARCADE;


So in short, the enemy travels across the `x` axis and once it collides with the player it triggers a function which looks like this, my update function:



then the function it calls, `collisionHandler` looks like 

function collisionHandler () {
    killedEnemies += 1;
    c.alive = false;
    text.setText("You have killed " + killedEnemies + " enemies !");


But like I said, it only kills the first item it hits, am I missing anything?

Link to comment
Share on other sites

16 hours ago, samme said:

You need to work with the colliding objects themselves:

function collisionHandler (player, enemy) {
    // …



Ahh I see. I did this, which works but for only the first one again. Basically, my `collisionHandler` only gets fired once after that even putting a `console.log()` in there doesn't work so it's not getting triggered.

Link to comment
Share on other sites

  • 2 months later...

  • Recently Browsing   0 members

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