Sign in to follow this  
thisisandreww

Polygon collision hitTest (getting array element?)

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 + ', ';
			}
		}
	},

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.