Double jumping

Subscribe to Double jumping 13 posts

avatar for Theevildolphin Theevildolphin 7 posts
Flag Post

I’m attempting to make a character in my game that get’s the ability to doubly jump, i’m fairly new to as2 and have only recently been using it, Here’s my character’s normal jump coding…

if (Key.isDown(Key.UP) && touchingGround) {
grav = maxJump;
}

and here’s my variables:

ar ground:MovieClip = _root.ground1;
var grav:Number = 0;
var gravity:Number = 0.5;
var maxJump:Number = -5;

Could i get some advice on how to get a character to do a double jump please?

 
avatar for Khronosis Khronosis 130 posts
Flag Post

Perhaps create another boolean variable called “doubleJumped” that keeps track of whether you’ve already jumped once while in the air.
Do something like:

if(Key.isDown(Key.UP) && !touchingGround && !doubleJumped)
{
    doubleJumped = true;
    grav = maxJump;
}

Then set “doubleJumped” back to false when you touch the ground again;

if(touchingGround && doubleJumped)
{
    doubleJumped = false;
}
 
avatar for alecz127 alecz127 817 posts
Flag Post

I don’t know AS2 very much, but I can help out with this one.
Khronosis is on the right page, but let me explain it a bit better. (sweet name and avatar btw Khro)

You have it set up so that the player is allowed to jump Only when the player presses up, and is touching ground.
To allow the player a 2nd jump while not touching ground is fairly straight forward.

Just make sure you apply some logic sauce to the mix.
Count how many times hes jumped with this variable.

var numJumps:int = 0;

Your if is a lot longer now, because of all that logic sauce. But it makes sense.
If Key up is being pressed && is touching the ground or key up is being pressed and the number of jumps that has happened is less than or equal to 2…
Than Jump!!!
Add one to the variable tracking how many jumps has happened.

if (Key.isDown(Key.UP) && touchingGround || Key.isDown(Key.UP) && numJumps <= 2) {
        grav = maxJump;
}
if(touchingGround){
      numJumps = 0;
}

Of course this means more variables, and it looks more complicated depending on how you look at it.
But I like this approach. : D
good luck.

 
avatar for Aaants Aaants 158 posts
Flag Post

It’s worth pointing out that you’ll also need to make sure they release the up key between jumping and double jumping. Not many players manage to push and release a key in one frame, so your double jump will happen instantly more often than not

 
avatar for Theevildolphin Theevildolphin 7 posts
Flag Post

Thanks for all the help, I’ve added the code, thanks alecz but it seems to allow him to jump infinite times, i’ve tried toying around with the code but only succeeded in bugging the character or making his jump sizes smaller. I think the problem is that there’s no difference between the first jump and the second jump so it still counts the second jump as an original jump and allows you to keep jumping :/

 
avatar for Khronosis Khronosis 130 posts
Flag Post

Can you post your new code so we could take a look?

If it’s letting him jump infinite times, then it’s also possible the problem is with the incrementation of “numJumps”. You could verify by using trace to check the current value after each jump.

 
avatar for Theevildolphin Theevildolphin 7 posts
Flag Post

Variables:

var ground:MovieClip = _root.ground1;
var grav:Number = 0;
var gravity:Number = 0.5;
var speed:Number = 3;
var maxJump:Number = -5;
var touchingGround:Boolean = true;
var numJumps:Number = 2;

And here’s the jumping code:

if (Key.isDown(Key.UP) && touchingGround || Key.isDown(Key.UP) && numJumps <= 2) {
grav = -5;
}
if(touchingGround){
numJumps = 2;
}

 
avatar for Khronosis Khronosis 130 posts
Flag Post

1. Since you’re using alecz’s method, “numJumps” should be set to 0 by default, not 2. He’s counting the number of times you have jumped, not the remaining jumps left. Alternatively, if you do want to start at 2, you’ll need to change “numJumps <= 2” to “numJumps > 0”.

2. You don’t seem to be incrementing “numJumps”, so it’s never changing.


Here’s the changes:

if (Key.isDown(Key.UP) && touchingGround || Key.isDown(Key.UP) && numJumps <= 2)
{
    numJumps++;    //increment numJumps
    grav = -5;
}
if(touchingGround)
{
    numJumps = 0;  //start at 0, not 2
}

 
avatar for Theevildolphin Theevildolphin 7 posts
Flag Post

He’s still jumps as many times as he can. I’ve copied your code and when that didn’t work i tried toying with all the variables. no matter what value i put into the numJumps he just keeps jumping. :\

 
avatar for BraydenBlack BraydenBlack 271 posts
Flag Post

did you set touchingGround to false when he jumps, and set it back to true when he lands?

 
avatar for Khronosis Khronosis 130 posts
Flag Post

trace() is your friend.


Try placing this inside the jump code:

trace(numJumps);

After each jump, you’ll see the current value of “numJumps” in the output, and that should indicate whether it’s working properly.

 
avatar for Theevildolphin Theevildolphin 7 posts
Flag Post

Ok so i managed to get the double jump working but you have to press the second jump almost instantly, does anyone know of a way to make the time that you are allowed to do the second jump longer?

 
avatar for alecz127 alecz127 817 posts
Flag Post
Originally posted by Theevildolphin:

Ok so i managed to get the double jump working but you have to press the second jump almost instantly, does anyone know of a way to make the time that you are allowed to do the second jump longer?

Sorry about the earlier code, I completely space the need for a second press of the up key. I didn’t personally compile the code myself friend.
As for your new question.
You need a timer! : D
I don’t know how this works in AS2 regretfully.
But in AS3

you instantiate a new timer

var jumpT:Timer = new Timer(3000); //the code next to me is a timer that runs and activated a function every 3
//seconds. Because its 3000 milliseconds which adds up to 3 seconds respectively.

//you than add an event listener to the new timer that will activate the function after 3 seconds.
//I can't remember what you replace blablabla with at this moment, but it will most likely be completely different in
//as2 any way. This is just my guiding you, look up how to use timers in AS2
jumpT.addEventListener(blablabla, functionName);

public function functionName(e:TimerEvent){
        allowJump = true;
}

//You might need to change the way your double jump code works.
//I'm off to get some mad elixir (faygo) good luck friend!