Jump to content

MoveToPointer, but only horizontally?


MelSmits
 Share

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)	{
			game.physics.arcade.moveToPointer(player,400);
			player.animations.play('left');
			if (Phaser.Rectangle.contains(player.body,game.input.x,game.input.y))	{
					player.body.velocity.setTo(0,0);
				}
		}

Here are my player properties:

player = game.add.sprite(32,game.world.height - 102, 'dude');
	game.physics.enable(player);
	player.anchor.SetTo(0.5,0.5);

	//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? 

Link to comment
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

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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