Jump to content

How to move to the point of clicks at p2js


spiritabsolute
 Share

Recommended Posts

Hi all! How to move to the point of clicks at p2js? 

this.game.input.onDown.add(this.onClick, this);

In the event handler I have "pointer.position". What's next? I did not find a single example. :(

 

I found a similar question here, but there is no answer to it.

http://stackoverflow.com/questions/36120249/precision-moving-a-body-using-phaser-and-p2-js-physics

Link to comment
Share on other sites

I realized this:

movePlayer: function() {
	this.player.directionX = this.game.input.activePointer.worldX - this.player.x;
	this.player.directionY = this.game.input.activePointer.worldY - this.player.y;
	this.player.endMovePointX = this.game.input.activePointer.worldX;
	this.player.endMovePointY = this.game.input.activePointer.worldY;
	this.player.body.rotation = Math.atan2(this.player.directionY, this.player.directionX) + this.game.math.degToRad(-90); 
	var angle = this.player.body.rotation + (Math.PI / 2);
	this.player.isMoves = true;
	this.player.body.velocity.x = this.player.walkSpeed * Math.cos(angle);    
	this.player.body.velocity.y = this.player.walkSpeed * Math.sin(angle);
},


update: function() {
	if(this.player.isMoves)
	{
		if(this.math.sign(this.player.directionX) === 1)
		{
			if(this.player.x > this.player.endMovePointX)
				this.player.body.velocity.x = 0;
		}
		else
		{
			if(this.player.x < this.player.endMovePointX)
				this.player.body.velocity.x = 0;
		}
		if(this.math.sign(this.player.directionY) === 1)
		{
			if(this.player.y > this.player.endMovePointY)
				this.player.body.velocity.y = 0;
		}
		else
		{
			if(this.player.y < this.player.endMovePointY)
				this.player.body.velocity.y = 0;
		}
	}
	else
	{
		this.stopMovePlayer();
	}
},

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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