MoveToPointer, but only horizontally?

Recommended Posts

I have a game where the player object is controlled by holding down the mouse to either side of it. The player then moves towards the pointer, and stops. 

However, I want this to work somewhat like Breakout, in that the player is not allowed to jump or move vertically in any way even if the cursor is above him. I can't seem to restrict y-movement, so I'm probably missing something! 

Here is my code for the player moving left. Right is exactly the same only opposite.  

	if (game.input.mousePointer.isDown && game.input.activePointer.x < player.x)	{
			if (Phaser.Rectangle.contains(player.body,game.input.x,game.input.y))	{

Here are my player properties:

player = game.add.sprite(32, - 102, 'dude');

	//Player physics
	player.body.collideWorldBounds = true;

	//Walk animations
	player.animations.add('left', [0,1,2,3],10,true);
	player.animations.add('right', [5,6,7,8],10,true);


With the current code, the player follows the cursor everywhere (as long as the button is down), until it hits a wall or platform with collision. I've thought about putting an invisible object over his head so that he can't move up, but it just seems like there should be another way! Can someone help me out? 

Share this post

Link to post
Share on other sites

Thanks drhayes, that did the trick! 

Stupid thing is I actually looked at that function as a possible solution but thought "nah that will just teleport the player there".  But of course that's just player.x = whatever

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.

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.


  • Recently Browsing   0 members

    No registered users viewing this page.