Jump to content

Polygon collision hitTest (getting array element?)


thisisandreww
 Share

Recommended Posts

I've started using Phaser for just about a week and I'm trying to develop something little. I apologize if the following code is messy.
Basically I'm trying to get the array's element when I click on the sprite with p2 polygon collision. Could anyone please point me to how I can do that? Thanks.

this.car = ["BMW", "Honda", "2","3","4"];
for(var i = 0; i < this.car.length; i++){
	this.car[0] = game.add.sprite(100, 100+(i*100), 'car');
	this.car[0].frame = 0;
	game.physics.p2.enable([this.car[i]],true);
}

click:function(pointer){
		var x=game.camera.x +pointer.position.x;
		var y=game.camera.y +pointer.position.y;
        for(var i = 0; i < this.car.length; i++){
		this.bodies = game.physics.p2.hitTest({x:x,y:y},[this.car[i]]);
        }

		if (this.bodies.length === 0)
		{
			this.result = "You didn't click a body";
		}
		else
		{
			this.result = "You clicked: ";
			for (var i = 0; i < this.bodies.length; i++)
			this.result = this.result + // What should I write here to show that I've clicked on '2' / 'BMW' etc. ??

			if (i < this.bodies.length - 1)
			{
				this.result = this.result + ', ';
			}
		}
	},

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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