Sign in to follow this  
spiritabsolute

How to move to the point of clicks at p2js

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

Share this post


Link to post
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();
	}
},

 

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.