Jump to content

Getting objects in a group to collide


Recommended Posts

I am using a portion of the tank games demo that creats a group in a different way


I have this at the top of my code

var EnemyTank = function (game) {    var x = game.world.randomX;    var y = game.world.randomY;    this.tank = game.add.sprite(x, y, 'enemy');    this.tank.anchor.set(0.5);    game.physics.enable(this.tank, Phaser.Physics.ARCADE);    game.physics.arcade.collide(this.tank,cat);    this.tank.body.immovable = false;    this.tank.body.collideWorldBounds = true;    this.tank.body.bounce.setTo(1, 1);    this.tank.angle = game.rnd.angle();    game.physics.arcade.velocityFromRotation(this.tank.rotation, 100, this.tank.body.velocity);    game.physics.arcade.collide(this.tank);};

'this is in the create

         enemies = [];            enemiesTotal = 20;            enemiesAlive = 20;           for (var i = 0; i < enemiesTotal; i++)           {             enemies.push(new EnemyTank(this.game));           }

And this is in the update


The objects are not bouncing off of each other. Any suggestions?

Link to comment
Share on other sites

  • 3 weeks later...

I would also like to see a solution for this.


In other game frameworks each object can have a type and collision can then be defined by type. So its easy to have a group of objects colliding with each other and maybe another group of objects. I dont know if thats possible with phaser.

Link to comment
Share on other sites

Just my guess here.


collide is meant to be called each time you want to test for collision and trigger reactions. (e.g on every update).


Secondly collide expects the two objects that you want to test for collision.


so remove your collision stuff from the Enemytank object, and place this in your update callback.



(what is 'cat'?)


and this

this.game.physics.arcade.collide(this.tank, enemies);

You may also want to add a callback as a third parameter if you want to be notified of the collision.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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