1
votes

With Phaser (and following allong with the tutorials) jumping is super-easy with the provided Arcade physics module. However, it has the annoying-to-me fact that a player can only jump when they are "on" the ground due to the use of player-is-touching-down logic.

The code I am using is based on Game Mechanic Explorer, Platformer #4 and follows;

var canJump = this.player.body.touching.down;

if (canJump && this.upInputIsActive()) {
    this.player.body.velocity.y = this.JUMP_SPEED;
}

I would like to enable a player be able to jump when they are really close to the ground but are not yet touching so that the re-jump/jump-as-landing is more fluid. As such, and the observation that the limit is caused by touching.down of the player, my questions are;

  1. Is it possible to determine the distance ("how high") the player is from the ground (which can be a platform) immediately below them? Then canJump is just a function of the distance, being true for very small values only.

  2. Can a virtual hit-box be extended around the player (that would travel-with the player) such that it's own touching.down could be used to detect this case? Is this approach better? The player sprite itself should not levitate off the ground or otherwise be effected.

The goal here is specifically to allow the player to jump just before they ought to be able to - so remembering an up key-press for a short duration of time, which may be viable, is not a solution I am seeking in this particular case.

1
You have to create a timer wantsToJump and set it to a specific time or number of steps when the user tries to jump while still in air, then count it down during update. As long as wantsToJump is greater than 0 the player should jump instantly when touching ground is detected.LearnCocos2D
@LearnCocos2D That is the current behavior if keeping the up button pressed (and such an approach would indeed replicate it for a single press within a small margin of error), but my goal is to actually allow the player to "cheat/game" the system by a few pixels. In my current game the player bounces a tiny bit (and falls slower) than in the linked example.user2864740
I wouldn't go down that road, if you do it means that a regular jump can reach a height of 100 pixels but if you do a continuous jump the second jump may reach a height of 105 pixels, possibly allowing the player to go to places she isn't supposed to. Plus it probably won't look/feel any better. Players will expect the player to jump again when touching ground, not when slightly above it. Perhaps you have a 1-frame delay between registering ground touch and jumping again that causes a small delay before re-jumping?LearnCocos2D
@LearnCocos2D If the player can get there, then they ought to be able to get there :D At this point I'm still looking for good solutions to the two questions (to be able to do these should-be-simply tasks) and not necessarily finish a polished game. I've managed to create a hackish #2 by manually moving another tiny sprite below the player and checking for overlap with the world objects, but it feels like a kludge. Thanks for the feedback about potential issues though, I'll keep it in mind.user2864740

1 Answers

3
votes
  1. Not "natively". There is no CCD or look-ahead, a body is only concerned with the direction it is travelling. What you're trying to achieve is commonly known as using a sensor. P2 has support for sensors, but Arcade Physics doesn't, so you'd have to simulate it - either by creating a second body (which you've alluded to in point 2) or using a marker. This could be a simple Point object that is 'dropped' when they jump and you can then use the built in distance checks between the point and the body to allow for your 'near jump' requirement.

  2. Yes it can, but your Sprite will then 'float' in the air. So see point 1 :)