AramCP Posted November 18, 2016 Share Posted November 18, 2016 Hi, im new in the forum and im learning to use phaser. Well, im making a Pokemon 2D game and all i did works nice, but i want to delete diagonal movement. I dont want to make a grid movement system, just delete the diagonal one. This is the code used in the update function: function update() { jugador.body.velocity.x = 0; jugador.body.velocity.y = 0; if (cursors.left.isDown) { jugador.body.velocity.x = -80; jugador.animations.play('izquierda'); } if (cursors.right.isDown) { jugador.body.velocity.x = 80; jugador.animations.play('derecha'); } if (cursors.up.isDown) { jugador.body.velocity.y = -80; jugador.animations.play('arriba'); } if (cursors.down.isDown) { jugador.body.velocity.y = 80; jugador.animations.play('abajo'); } } Well as you can see its a basic sprite movement, but when i press down and left it moves in diagonal, and i dont want that. Thanks for helping me. Link to comment Share on other sites More sharing options...
mattstyles Posted November 18, 2016 Share Posted November 18, 2016 Simplest is probably to just return after checking each key e.g. function update() { jugador.body.velocity.x = 0; jugador.body.velocity.y = 0; if (cursors.left.isDown) { jugador.body.velocity.x = -80; jugador.animations.play('izquierda'); return } if (cursors.right.isDown) { jugador.body.velocity.x = 80; jugador.animations.play('derecha'); return } if (cursors.up.isDown) { jugador.body.velocity.y = -80; jugador.animations.play('arriba'); return } if (cursors.down.isDown) { jugador.body.velocity.y = 80; jugador.animations.play('abajo'); return } } This sets an explicit order of preference, but that probably isn't an issue, you'd choose which direction when the users mashes 2 keys anyway, this way is simpler but you might want a more complex method that tracks current direction and only changes direction when their is no current direction. AramCP 1 Link to comment Share on other sites More sharing options...
AramCP Posted November 18, 2016 Author Share Posted November 18, 2016 2 hours ago, mattstyles said: Simplest is probably to just return after checking each key e.g. function update() { jugador.body.velocity.x = 0; jugador.body.velocity.y = 0; if (cursors.left.isDown) { jugador.body.velocity.x = -80; jugador.animations.play('izquierda'); return } if (cursors.right.isDown) { jugador.body.velocity.x = 80; jugador.animations.play('derecha'); return } if (cursors.up.isDown) { jugador.body.velocity.y = -80; jugador.animations.play('arriba'); return } if (cursors.down.isDown) { jugador.body.velocity.y = 80; jugador.animations.play('abajo'); return } } This sets an explicit order of preference, but that probably isn't an issue, you'd choose which direction when the users mashes 2 keys anyway, this way is simpler but you might want a more complex method that tracks current direction and only changes direction when their is no current direction. Thanks man i will try it, but i have a question, what exatly does that return? Im learning to program and im so noob i know, but i read somewhere that the return command stops the execution of a function and returns a value from that function. But that return is inside a conditional, not inside a function, so i dont get it at all, if u could solve me that i would thank you so much man! Edit: It works!! Link to comment Share on other sites More sharing options...
mattstyles Posted November 19, 2016 Share Posted November 19, 2016 18 hours ago, AramCP said: Thanks man i will try it, but i have a question, what exatly does that return? Im learning to program and im so noob i know, but i read somewhere that the return command stops the execution of a function and returns a value from that function. But that return is inside a conditional, not inside a function, so i dont get it at all, if u could solve me that i would thank you so much man! No worries, thats a good question! The conditional is a block, and that block has a parent block, which in this case is a function, the update function. Returning from anywhere inside the function will indeed stop the execution of that function, whereby control passes back to whatever called the function and your program continues, doesn't matter if thats in the parent block scope or a child block scope (in this case, the conditional block). note: There are certain cases where this won't be the case but that's a broader discussion, usually when you embed functions within other functions, which usually isn't a great idea anyway. In this case this is an implicit return because you haven't specified what the function returns, so javascript returns 'undefined', kind-of akin to null or void in other languages that require an explicit return. In your case, you only want the code for one key to be active at a time so by using 'early return' you force your function to return before the other conditions get checked, which gives you the functionality you want. Early return is a good pattern, its cleaner to write, which is the main benefit, it is also very very fractionally more performant as you perform less comparisons and evaluations. AramCP 1 Link to comment Share on other sites More sharing options...
Recommended Posts