How do I create a double jump in code.org?

119 Views Asked by At

I am trying to create a game where I can double or triple jump, similar to super smash bros. I was wondering how I can make a "double jump" feature using Code.org? This is what I currently have. The variables are defined.

function jumping() {
    if (keyWentDown("w")) {
        player.setAnimation("playerJump");
        player.velocityY = -jumpForce;
        setTimeout(function() {
            player.setAnimation("player");
            setTimeout(function() {}, 400);
        }, 400);
    }
    if (keyWentUp("w")) {
        jumpcount = jumpcount + 1;
    }
    if (jumpcount == 3) {
        jumpcount = 0;
    }
}



function PlayerGravity() {
    player.velocityY += gravity;
}
1

There are 1 best solutions below

0
Evan Troxell On

You may not still need the answer, but here is my take on it:

Create 2 variable: The variable 'maxJumps' which defines the maximum number of jumps at one time, and the variable 'jumpCounter' which defines the current number of jumps. Create a separate function 'isStanding' that checks if the player is standing on the ground, or is in in air. If you have a platform style game, you may need to check multiple platforms to do this. Every iteration of the game, run the 'isStanding' function to tell if the player is standing. If the player is standing, set 'jumpCounter' to 0 and reset the player's velocity. When the player presses the 'w' key, check if 'jumpCounter' is less than 'maxJumps', and run the rest of the code for jumping if so. This is what that looks like:

// Definition of gravity value, jumpForce value, jumpCounter value,
// maxJumps value, isJumping value, and player value
var gravity = 2,
jumpForce = 18,
jumpCounter = 0,
maxJumps = 2,
isJumping = false,
player = createSprite(0,0);
player.setAnimation("player");

// Draw loop
function draw() {
    background(rgb(255,255,255));
    jumping();
    playerGravity();
    isStanding();

    drawSprites();
}

function jumping() {
    if (keyWentDown("w") && jumpCounter < maxJumps) {
        isJumping = true;
        player.setAnimation("playerJump");
        player.velocityY = -jumpForce;
        // I moved the animation resetter to the 'isStanding' function
        // so the player's animation is only reset once they are
        // guaranteed to be standing 
        jumpCounter = jumpCounter + 1;
    }
}

function isStanding() {
    if ((player.y == 200)) {
        jumpCounter = 0;
        isJumping = false;
        player.setAnimation("player");
        player.velocityY = 0;
    }
}

function playerGravity() {
    player.velocityY += gravity;

    // Added this for clarity, don't include if you have this written
    player.y = Math.min(player.y + player.velocityY, 200);
}