shtanton Posted May 19, 2016 Share Posted May 19, 2016 I'm making a top down game where the player moves smoothly around the plane but is confined to a 64x64 grid, I have achieved this by allowing them to move if they are exactly positioned on one of the 64x64 tiles and if they are, the game tweens them to the appropriate tile. The problem is that despite my best efforts it is still possible to move diagonally which would cause a lot of bugs in my collision code. This is the code I am using update: function () { if (player.x%64 === 0 && player.y%64 === 0) { if (game.input.keyboard.isDown(Phaser.KeyCode.W)) { game.add.tween(player).to({y: player.y-64}, 250, Phaser.Easing.Linear.None, true); } else if (game.input.keyboard.isDown(Phaser.KeyCode.S)) { game.add.tween(player).to({y: player.y+64}, 250, Phaser.Easing.Linear.None, true); } else if (game.input.keyboard.isDown(Phaser.KeyCode.A)) { game.add.tween(player).to({x: player.x-64}, 250, Phaser.Easing.Linear.None, true); } else if (game.input.keyboard.isDown(Phaser.KeyCode.D)) { game.add.tween(player).to({x: player.x+64}, 250, Phaser.Easing.Linear.None, true); } } }, Any help would be great! Thanks in advance Link to comment Share on other sites More sharing options...
Umz Posted May 19, 2016 Share Posted May 19, 2016 I'd suggest add a state to the Player that checks if it's currently moving. Until it's finished don't allow any more Tweens to be added. The tween.onComplete.add(function() {}) can be used to reset the player state. in mono 1 Link to comment Share on other sites More sharing options...
VitaZheltyakov Posted May 19, 2016 Share Posted May 19, 2016 It is bad practice to use animation instead of physics. Use this example: http://phaser.io/examples/v2/arcade-physics/bounce-knock Link to comment Share on other sites More sharing options...
Umz Posted May 19, 2016 Share Posted May 19, 2016 1 hour ago, VitaZheltyakov said: It is bad practice to use animation instead of physics. Use this example: http://phaser.io/examples/v2/arcade-physics/bounce-knock What ? Sorry.. Where, When and Who did this information originate from? You use physics if your game requires physics. shtanton 1 Link to comment Share on other sites More sharing options...
shtanton Posted May 20, 2016 Author Share Posted May 20, 2016 9 hours ago, VitaZheltyakov said: It is bad practice to use animation instead of physics. Use this example: http://phaser.io/examples/v2/arcade-physics/bounce-knock I couldn't find a method that interacted with my map the way I intended it to, I have a layer in the json map called hitboxes that either has 0 or a value for whether the sprite should collide with it, if you have a way that phaser can do this with the built in physics and still stay in the 64x64 grid alignment then I'm open to suggestions, thanks! Link to comment Share on other sites More sharing options...
VitaZheltyakov Posted May 20, 2016 Share Posted May 20, 2016 2 hours ago, shtanton said: I couldn't find a method that interacted with my map the way I intended it to, I have a layer in the json map called hitboxes that either has 0 or a value for whether the sprite should collide with it, if you have a way that phaser can do this with the built in physics and still stay in the 64x64 grid alignment then I'm open to suggestions, thanks! Look at the player moves as in the example. This is what you need. Link to comment Share on other sites More sharing options...
shtanton Posted May 20, 2016 Author Share Posted May 20, 2016 6 hours ago, VitaZheltyakov said: Look at the player moves as in the example. This is what you need. The example still allows the player to move diagonally which is what I'm trying to avoid, I don't know why the "else if" statements aren't doing this Link to comment Share on other sites More sharing options...
fillmoreb Posted May 20, 2016 Share Posted May 20, 2016 Umz has got the right solution. Here's a fiddle I made a while back to answer a similar question. It should demonstrate how to accomplish what you want. Good luck. https://jsfiddle.net/mub2ps0c/7/ shtanton 1 Link to comment Share on other sites More sharing options...
shtanton Posted May 21, 2016 Author Share Posted May 21, 2016 16 hours ago, fillmoreb said: Umz has got the right solution. Here's a fiddle I made a while back to answer a similar question. It should demonstrate how to accomplish what you want. Good luck. https://jsfiddle.net/mub2ps0c/7/ Thanks to both fillmoreb and umz! Link to comment Share on other sites More sharing options...
Recommended Posts