frenetikm Posted August 6, 2015 Share Posted August 6, 2015 Hi, I searched the forum and examples, but I didn't find anything about this:if (this.cursors.up.isDown && this.player.body.onFloor()) { this.player.body.velocity.y = -400;}It always doing the same jump, what I would like is a jump depending on key pression. What is the correct and best way to do this? Link to comment Share on other sites More sharing options...
Tom Atom Posted August 6, 2015 Share Posted August 6, 2015 Hi, not sure it will work - try yourself: first little theory - when you press jump button, you are instantly changing object's velocity. You are giving it impulse. Then physics simulation during time applies forces on objects in simulation. One of these forces is gravity. As velocity is affected with acceleration and gravity is acceleration, you object's velocity is step by step decreasing (it is slowing down on jump top) until it gets opposite sign and object starts falling down. So, I think you can try to check if up key is down, but you are NOT on floor. In such case apply force to object in original direction of jump (upwards). This applied force should decrease gravity effect, so jump will be higher. If your force will be too big (to overcome gravity) you will get "jetpack" :-) I would try to apply it like:this.player.body.velocity.y += my_up_force * game.time.physicsElapsed(where my_up_force is negative to point up). Tilde and frenetikm 2 Link to comment Share on other sites More sharing options...
frenetikm Posted August 6, 2015 Author Share Posted August 6, 2015 Thanks ! Solved like this :if (this.cursors.up.isDown && this.player.body.onFloor()) { this.player.body.velocity.y = -250; this.player.force = 200;} else if(this.cursors.up.isDown && this.player.force > 0) { this.player.body.velocity.y -= 25; this.player.force -= 25;}I don't know game.time.physicsElapsed so I prefer not using it Link to comment Share on other sites More sharing options...
frenetikm Posted August 6, 2015 Author Share Posted August 6, 2015 a little add to be sure that the jump is one movement and can't be continued after releasing the keyif (this.cursors.up.isDown && this.player.body.onFloor()) { this.player.body.velocity.y = -250; this.player.force = 200; this.player.jumping = true;} else if(this.cursors.up.isDown && this.player.force > 0 && this.player.jumping) { this.player.body.velocity.y -= 25; this.player.force -= 25;} else if(!this.cursors.up.isDown || this.player.force <= 0) { this.player.jumping = false;} Link to comment Share on other sites More sharing options...
Recommended Posts