PhasedEvolution Posted September 8, 2016 Share Posted September 8, 2016 Hello. I have included character movement in my phaser isometric game. This is the code that allows the character to move: // INSIDE CREATE // this.cursors = game.input.keyboard.createCursorKeys(); this.game.input.keyboard.addKeyCapture([ Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT, Phaser.Keyboard.UP, Phaser.Keyboard.DOWN, ]); // INSIDE UPDATE // var speed = 100; if (this.cursors.up.isDown) { dude.body.velocity.y = -speed; dude.body.velocity.x = -speed; } else if (this.cursors.down.isDown) { dude.body.velocity.y = speed; dude.body.velocity.x = speed; } else { dude.body.velocity.y = 0; dude.body.velocity.x = 0; } if (this.cursors.left.isDown) { dude.body.velocity.x = -speed; dude.body.velocity.y = speed; } else if (this.cursors.right.isDown) { dude.body.velocity.x = speed; dude.body.velocity.y = -speed; } I can actually move my character, but two problems happen: The response time is not that good. I notice that when I click one of the arrow keys my character doesn't move exactly when I click it. Not like in this example: http://rotates.org/phaser/iso/examples/character.htm . Sometimes, when I just click one of the arrows and release it the character continues to move continously. I want my character not to be fixed to the isometric grid like in the example. That is why I have those extra commands in relation to the example I showed. Any help with this? Thank you Link to comment Share on other sites More sharing options...
lumoludo Posted September 8, 2016 Share Posted September 8, 2016 I'm not sure why your character would not start moving immediately. You made need to show a demo so that we can see the problem in action. The reason your character keeps going is probably due to the way you are zeroing out your velocity. Because you want to affect the X and Y axis at the same time, you may want to change your code a bit. I would suggest something like this: (Note that the velocity is zeroed at the start of the update function, and the velocity values are cumulative in the key check code) update:function() { var speed = 100; dude.body.velocity.x = 0; dude.body.velocity.y = 0; if (this.cursors.up.isDown) { dude.body.velocity.y -= speed; dude.body.velocity.x -= speed; } else if (this.cursors.down.isDown) { dude.body.velocity.y += speed; dude.body.velocity.x += speed; } if (this.cursors.left.isDown) { dude.body.velocity.x -= speed; dude.body.velocity.y += speed; } else if (this.cursors.right.isDown) { dude.body.velocity.x += speed; dude.body.velocity.y -= speed; } } Link to comment Share on other sites More sharing options...
PhasedEvolution Posted September 8, 2016 Author Share Posted September 8, 2016 Well I understood the part of setting the velocity to 0 as default the way you said. Indeed makes more sense. I didn't understand why you make the values cumulative as the velocity indicates how fast he is moving, not the position I think... However I am starting to think that the problems I have are not "my fault". If early on I used the exact same code as in the example and still had this problem I can only assume it happens due to external factors. In any case I am using Microsoft Edge. Can it be related to that? Sometimes I click the arrow and release and the character moves. Other times it doesnt Link to comment Share on other sites More sharing options...
lumoludo Posted September 8, 2016 Share Posted September 8, 2016 I suggested accumulating the velocity primarily because you wanted to move up/down/left/right in the isometric world coordinates. This will allow your player to still use more than one arrow key at time. In your original code, the left and right arrows would override the down and up arrows instead of using both directions at the same time. Accumulating velocity in this instance is OK because it is zeroed before hand. If it was not zeroed, then it would accelerate way too fast. Does the example code (the link you posted) work correctly in the Edge browser? That would be an easy way to test if the problem is in your code or your browser. You could also try your code in multiple browsers. It's a good practice to get into as well, as your players will certainly be using a variety of browsers, and all of the common browsers are free to download. PhasedEvolution 1 Link to comment Share on other sites More sharing options...
PhasedEvolution Posted September 8, 2016 Author Share Posted September 8, 2016 Understood. Makes all sense now. It was a browser problem. It runs just fine in the other browser. I had a "Interference" in edge nothing special. Thank you for your support, really. Link to comment Share on other sites More sharing options...
Recommended Posts