MelSmits Posted June 8, 2016 Share Posted June 8, 2016 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 More sharing options...
drhayes Posted June 8, 2016 Share Posted June 8, 2016 What about game.physics.arcade.moveToXY where you get the X from the pointer but set the Y to the current Y of the player sprite? WombatTurkey and MelSmits 2 Link to comment Share on other sites More sharing options...
MelSmits Posted June 9, 2016 Author Share Posted June 9, 2016 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 More sharing options...
Recommended Posts