[SOLVED] Collisions Are Fired In All Objects At The Same Time


(Zouhair Elamrani Abou Elassad) #1

Hi, i’m adding multiple objects from the same class with a type, i check in the class update method collision with my player, but once the collision is fired, it’s fired in all the objects from the same class not only the object who collided with the player :

 public function Trap(x:Number=0, y:Number=0) 
	{
		
		trace("door trap constructor");
		super(x,y);

		layer = 1;
		type = "Trap";
	}

in the update method :

   if (_player.collide("Trap", _player.x, _player.y)) {
			trace("trap has been hit");				
		}

Is there a way to fire collision only for the object that collided with player !


(Martí Angelats i Ribera) #2

Yes. And you know it exists.

var trap:Trap = _player.collide("Trap", _player.x, _player.y) as Trap;

It actually returns the first entity that colides with the player.

If you want to have all of them, just use:

var traps:Vector.<Entity> = new Vector.<Entity>;
collideInto("Trap", _player.x, _player.y, traps);

//traps have all the colliding entities wich has the type "Trap".

PS: Next time look at the documentation a bit before asking, please.


(Zouhair Elamrani Abou Elassad) #3

Actually i do look in the documentation, that’s why i’m a bit confused, this is what i did :

var trap:Trap = _player.collide("Trap", _player.x, _player.y) as Trap;
		if (trap) {
			trace("trap has been hit");			
		}

But still, the collision is fired as many times as the number of trap entities i have in stage even though it hits only one.


(Darrin) #4

can you show more code? Collision is solid. Note your trace will keep triggering while the player is standing on the trap. In other words, it will trace every frame even when touching just 1 trap.

trap has been hit

trap has been hit

trap has been hit

trap has been hit

trap has been hit

trap has been hit

until you move off it. You may just need to remove that trap entity.


(JP Mortiboys) #5

I can see several things wrong here.

Firstly, in your last bit of code, you’re using if (Trap). Trap is the class name; it’s always going to exist. You want trap - the variable you just created.

Second, you’re checking the player’s collision against EVERY trap, inside EVERY trap’s update - of course you’re going to get multiple results.

Either check against all traps in the PLAYER’s update, or check the player against the SPECIFIC trap in the trap’s update, not against all of them.


(Zouhair Elamrani Abou Elassad) #6

About the class name it was just a typo mistake:p, sorry for that. As for checking collision, you said i could check the player against the SPECIFIC trap in the trap’s update, i thought that what i was doing, could please explain how to do that, Thank You


(Zouhair Elamrani Abou Elassad) #7

In fact i checked, the number traces equals exactly the number of entities, i added and removed entities to test, as for the code, that’s about it, i’m just checking collisions that i want to fire when the player hits the object, and if i removed the trap entity( the entity of the collision) that will remove my object out of the stage as well


(Zouhair Elamrani Abou Elassad) #8

Ok, i found a solution, instead of checking collision : player --> trap , i checked : trap --> player :

Inside the Trap update :

var player:Player = collide("player", this.x, this.y) as Player;
		if (player) {
			trace("trap has been hit");		
		}

And the player has a type=“player” :smile:, Thank You Guys


(Zouhair Elamrani Abou Elassad) #9

I see what you mean, thanks mate :smile: