Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

How do I handle collisions with groups in this situation.


Blue-EyesWhiteDragon
 Share

Recommended Posts

I was experimenting with sprites constructed from separate images (or parts) and I came across something I didn't expect.

I have a Player entity constructed from 5 parts (that all layer together, without the need for body.offset) - torso, head, right/left leg, right/left arm.

 

Code:

/* Create the Player */Player = game.add.group();/* Player position */Player.x = game.world.width / 2;Player.y = game.world.height / 2;/* Start Player Physics */game.physics.arcade.enable(Player, true);/* Set Player Physics */Player.enableBody = true;Player.enableBodyDebug = true;Player.physicsBodyType = Phaser.Physics.ARCADE;/* Player parts */Player.parts = {	head: Player.create(0, 0, "head"),	torso: Player.create(0, 0, "torso"),	arms: {		left: Player.create(0, 0, "arm_left"),		right: Player.create(0, 0, "arm_right")	},	legs: {		left: Player.create(0, 0, "leg_left"),		right: Player.create(0, 0, "leg_right")	}}/* Player parts size (wow, that sounds dirty) */Player.parts.head.body.setSize(24, 20, 20, 12);Player.parts.torso.body.setSize(16, 16, 24, 32);Player.parts.arms.left.body.setSize(8, 18, 40, 32);Player.parts.arms.right.body.setSize(8, 18, 16, 32);Player.parts.legs.left.body.setSize(12, 14, 32, 48);Player.parts.legs.right.body.setSize(12, 14, 20, 48);

Now when I call the my collision handler (for overlap):

game.physics.arcade.overlap(TestEnemy, Player, function(){	Player.hurt(TestEnemy.damage);}, null, this);

TestEnemy.damage is set to 1 and Player.hurt is as following:

Player.hurt = function(n){	if(typeof n === "string" && n.indexOf("%") > -1){		var percent = (parseInt(n.replace("%", "")) / 100);		n = Math.floor(this.health * percent);	}	if((this.health -= Math.floor(parseInt(n))) <= -1){		this.health = 0;		this.alive = false;	}	else{		this.health -= Math.floor(parseInt(n));	}};

Now they're both groups, but when the TestEnemy projectile passes through the Player, it fires the collision handler for each of the group's children it hits - is there anyway for it to only fire for the group itself, and not the children recursively, if it touches the group?
 

post-15161-0-68829800-1443541093.png

post-15161-0-95024000-1443541093.png

post-15161-0-19982800-1443541094.png

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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