Jump to content

Collision between objects in the same group does not work


jonteferm
 Share

Recommended Posts

Hey!

I have a group of enemies created like this (called from create()):

spawnEnemies: function(map){
	this.enemies = this.game.add.group();
	this.enemies.enableBody = true;
	this.game.physics.arcade.enable(this.enemies);
		
	var enemyStartPositions = this.findObjectsByType('enemyStart', this.map, 'objectLayer');

	for(var i = 0; i < enemyStartPositions.length; i++){
		var enemyStart = enemyStartPositions[i];
		var enemy = new Enemy(this.game, enemyStart.x, enemyStart.y, 'cultist');
			
		enemy.countStats();
		this.enemies.add(enemy);
	}

	this.enemies.setAll("body.immovable", true);
}

In the update function - I try to detect the collision between the children in the group like in this example (using 2.4.7): http://examples.phaser.io/_site/view_full.html?d=arcade physics&f=group vs self.js&t=group vs self&phaser_version=v2.4.7&

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

But they wont collide with - just passing trough - each other. Also tried with adding a callback as when I let the player sprite collide with the enemy group like this:

this.game.physics.arcade.collide(this.player, this.enemies, this.collisionHandlerPlayerAndEnemy, null, this);
this.game.physics.arcade.collide(this.enemies, null, this.collisionHandlerEnemyAndEnemy);

And the callback-function will not be called (may it be that this does not even work at all when the case is group to group?).

I need someone elses eyes on this. No properties for enemy body is set outside these code blocks.

Link to comment
Share on other sites

Hi!

Try this:

spawnEnemies: function(map){
    this.enemies = this.game.add.group();
    this.game.physics.arcade.enable(this.enemies);

    var enemyStartPositions = this.findObjectsByType('enemyStart', this.map, 'objectLayer');

    for(var i = 0; i < enemyStartPositions.length; i++){
        var enemyStart = enemyStartPositions[i];
        var enemy = new Enemy(this.game, enemyStart.x, enemyStart.y, 'cultist');
        enemy.countStats();
        this.enemies.add(enemy);
    }
    this.enemies.setAll("body.immovable", true);
}

this.physics.arcade.collide(this.enemies);

this.physics.arcade.collide(
    this.player,
    this.enemies,
    this.collisionHandlerPlayerAndEnemy,
    null,
    this
);

this.physics.arcade.collide(
    this.enemies,
    null,
    this.collisionHandlerEnemyAndEnemy,
    null,
    this
);

Check your 'map' argument 'function(map)' and 'this.map'. Is 'this.map' a variable? If you want to make it a returning argument just write 'map'

I hope it helps!

Link to comment
Share on other sites

On 2017-01-25 at 2:50 PM, FinalFantasyVII said:

Hi!

Try this:


spawnEnemies: function(map){
    this.enemies = this.game.add.group();
    this.game.physics.arcade.enable(this.enemies);

    var enemyStartPositions = this.findObjectsByType('enemyStart', this.map, 'objectLayer');

    for(var i = 0; i < enemyStartPositions.length; i++){
        var enemyStart = enemyStartPositions[i];
        var enemy = new Enemy(this.game, enemyStart.x, enemyStart.y, 'cultist');
        enemy.countStats();
        this.enemies.add(enemy);
    }
    this.enemies.setAll("body.immovable", true);
}

this.physics.arcade.collide(this.enemies);

this.physics.arcade.collide(
    this.player,
    this.enemies,
    this.collisionHandlerPlayerAndEnemy,
    null,
    this
);

this.physics.arcade.collide(
    this.enemies,
    null,
    this.collisionHandlerEnemyAndEnemy,
    null,
    this
);

Check your 'map' argument 'function(map)' and 'this.map'. Is 'this.map' a variable? If you want to make it a returning argument just write 'map'

I hope it helps!

Aah! Thank you :) Yes, that looks a bit weird in this context. It is a variable global to the game. I don't need to pass map to the function at this point - obviously as I do not even use it xD . But in the future - I thought maybe I will have more map objects representing levels and stuff which will load it's separate enemies. But now when I think of it: Why? I can't imagine I will ever have to view more than one map at once.

On 2017-01-25 at 10:11 PM, samme said:

this.game.physics.arcade.collide(this.enemies);
// OR
this.game.physics.arcade.collide(this.enemies, this.enemies, callback);

should work but if all bodies are immovable they can't react to the collision, so you won't see anything happen.

(Collisions against null always fail.)

Yes, you are right they should not be immovable. I guess I thought of "immovable" as "impassable". Also, when trying the line with the callback - I made the arcade.collide call in the wrong way. Thank you! :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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