replit / kaboom

💥 JavaScript game library
https://kaboomjs.com
MIT License
2.66k stars 226 forks source link

add `jumpRelease` function to `BodyComp` #781

Closed joshuacurtiss closed 10 months ago

joshuacurtiss commented 10 months ago

Problem: When jumping with the jump function in BodyComp, a force is applied but thereafter you can't change the velocity. What about if you want to: (a) Change the height of jump based on how long the player holds down the jump key? (b) Want to stomp down in the middle of a jump?

Solution: With this function, you can accomplish the points mentioned above with a jumpRelease function that lets you change the velocity mid-jump.

Example of use can be seen here:
https://www.curtiss.me/build-mario-with-kaboom/

(Notice how short or long jumps can be achieved by how long you hold the jump button, or stomping down by pressing down mid-jump)

How it is accomplished in the sample:

Thanks for your consideration.

slmjkdbtl commented 10 months ago

What do you think about just expose vel? This should enable most things

slmjkdbtl commented 10 months ago

So you can just

// Accelerate falling when player holding down arrow key
onKeyDown("down", () => {
    if (!player.isGrounded()) {
        player.vel.y += dt() * 1200
    }
})

// Jump higher if space is held
onKeyDown("space", () => {
    if (!player.isGrounded() && player.vel.y < 0) {
        player.vel.y -= dt() * 600
    }
})
joshuacurtiss commented 10 months ago

Hi @slmjkdbtl! Yes, exposing vel would make me happy and would be more versatile. 🎉

slmjkdbtl commented 10 months ago

vel is exposed in v3000.1.17!