0
\$\begingroup\$

I have a dash function in my game as well as a jump function. Both of them works completely fine all by themselves. The jump works find and the dash works fine, however, I want to be able to jump cancel from my dash. During my dash, when I try to hit the jump button, the charater object moves horizontally instead of vertically. It almost acts as a second dash.

What I want to happen is the character to be able to jump vertically out of my characters dash. Can someone guide me in the right direction to my desired results?

if (keyboard_check(ord("D")) && !place_meeting(x + 2, y, obj_hitbox_wall)) {
    x += 2;
    image_xscale =+1;
    directionFacing = "Right";
}
if (keyboard_check(ord("A")) && !place_meeting(x - 2, y, obj_hitbox_wall)) {
    x -= 2;
    image_xscale =-1;
    directionFacing = "Left";
}
///// WALL HIT DETECTION START /////
if (!place_meeting(x,y+2,obj_hitbox_wall)){
    gravity = .25;
    grounded = false;
} else {
    vspeed = 0;
    gravity = 0;
    grounded = true;
    jumpDash = false;
}
///// WALL HIT DETECTION STOP /////

///// DOUBLE JUMP PROPERTIES START /////
if (grounded == false && jumped == true) {
    if (keyboard_check_pressed(vk_space)) {
        vspeed = 0;
        vspeed -= 5;
        jumped = false;
    }
}


if (grounded == true) {
    if (keyboard_check_pressed(vk_space)) {
        vspeed -=5;
        jumped = true;
    }
}
///// DOUBLE JUMP PROPERTIES END /////

///// DASH PROPERTIES START /////
if (dashed == false && keyboard_check_pressed(vk_shift) && jumpDash == false) {
    if (directionFacing = "Right") {
        vspeed = 0;
        gravity = 0;
        speed += 8;
        dashed = true;
    } 

    if (directionFacing = "Left") {
        vspeed = 0;
        gravity = 0;
        if (grounded == true) {
            speed -= 8;
        } else {
            speed+=8;
        }
        dashed = true;
    }

}

if (dashed == true && directionFacing = "Right") {
     speed -= .4;
     direction = 0;
    if (speed <= 0) {
        dashed = false;
        gravity = .25;
        speed = 0;
    }
}

if (dashed == true && directionFacing = "Left") {
    if (grounded == true){
        speed += .4;
        direction = 0;
    } else {
        speed -= .4;
        direction = 180;
    }
    if (grounded == true && speed >= 0) {
        dashed = false;
        gravity = .25;
        speed = 0
    } else if (grounded == false && speed <= 0) {
        dashed = false;
        gravity = .25;
        speed = 0
    }
}
///// DASH PROPERTIES END /////

///// JUMP DASH PROPERTIES START /////
if (grounded == false && dashed == true) {
    jumpDash = true;
}
///// JUMP DASH PROPERTIES END /////
\$\endgroup\$
4
\$\begingroup\$

I would recommend trying to clean up and reduce the number of variables you are keeping track of, as more bugs are likely to develop.

But on to your question:

The problem you are seeing is that jumping out of a dash appears to be acting like a second dash rather than a jump. This would tell me that there is a problem around how you are setting speed and vspeed.

The only place you are setting the character to move at "dashing speed" is in the dash block, which means this has to be evaluating to true:

if (dashed == false && keyboard_check_pressed(vk_shift) && jumpDash == false) {

So we can break this down to try to understand how that may resolve to true when you don't expect it to.

First, let's look at how dashed == false could evaluate to true.

The only places you are setting dashed to false are in this block (I've removed some of the code to expose the logic a little more clearly):

if (dashed == true && directionFacing = "Right") {
    if (speed <= 0) {
        dashed = false;
        gravity = .25;
        speed = 0;
    }
}

if (dashed == true && directionFacing = "Left") {
    if (grounded == true && speed >= 0) {
        dashed = false;
        gravity = .25;
        speed = 0
    } else if (grounded == false && speed <= 0) {
        dashed = false;
        gravity = .25;
        speed = 0
    }
}

Let's summarize the conditions. We set dashed to false if:

  1. Player is dashing right, and speed <= 0
  2. Player is dashing left and they are grounded with speed >= 0
  3. Player is dashing left and they are not grounded with speed <= 0

Next piece of logic is:

keyboard_check_pressed(vk_shift)

If you press shift, this is true.

Last piece is:

jumpDash == false

The only place you set this to false is in your collision logic:

if (!place_meeting(x,y+2,obj_hitbox_wall)){
    gravity = .25;
    grounded = false;
} else {
    vspeed = 0;
    gravity = 0;
    grounded = true;
    jumpDash = false;
}

So if the player is grounded, jump dash stays false.

The only variable to this bug seems to be how the speed variable is being set.

Putting it all together, it appears to me that this bug may only affect the player when moving left. My evidence to support this claim:

Player dashes left while grounded (hits this block of code):

    if (directionFacing = "Left") {
        vspeed = 0;
        gravity = 0;
        if (grounded == true) {
            speed -= 8;
        } else {
            speed+=8;
        }
        dashed = true;
    }

At this point speed -= 8 is hit and the player how has speed <= 0

While dashing, the player tries to jump by hitting space, triggering this block of code:

if (grounded == true) {
    if (keyboard_check_pressed(vk_space)) {
        vspeed -=5;
        jumped = true;
    }
}

You don't include the code where the player is actually being moved based on these variables, but since vspeed -= 5 was hit, I assume the player will be moved off of the ground.

After one loop, the collision check sees that the player is no longer grounded:

if (!place_meeting(x,y+2,obj_hitbox_wall)){
    gravity = .25;
    grounded = false;
} else {
    vspeed = 0;
    gravity = 0;
    grounded = true;
    jumpDash = false;
}

At this point we are in this state (roughly):

  • directionFacing = "Left"
  • dashed = true
  • speed <= 0
  • grounded = false

This sets us up to fall into this block of code:

if (dashed == true && directionFacing = "Left") {
    if (grounded == true){
        speed += .4;
        direction = 0;
    } else {
        speed -= .4;
        direction = 180;
    }
    if (grounded == true && speed >= 0) {
        dashed = false;
        gravity = .25;
        speed = 0
    } else if (grounded == false && speed <= 0) {
        dashed = false;
        gravity = .25;
        speed = 0
    }
}

Specifically this piece will be triggered:

} else if (grounded == false && speed <= 0) {
   dashed = false;
   gravity = .25;
   speed = 0
}

Which will set dashed = false

Once that happens, if you're still holding down vk_shift, the dash condition is now true again:

if (dashed == false && keyboard_check_pressed(vk_shift) && jumpDash == false)

Which will result in a dash instead of continuing to jump.

It looks like if you were to let go of shift, and then press space, you'd be able to jump out of a dash as expected.

Hopefully my reasoning through this made sense. There may be a couple gaps here since I don't know exactly how the variable are being used when your game is stepping the world.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Wow thank you very much for this very well thought out explanation. Currently at work, so I will rework my code according to your answer when I am home. Will let you know how it goes. \$\endgroup\$ – bmarkham Nov 23 '17 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.