Doubt about Jump
fgb01

Good morning people,
I could not understand making the jump in the platform tutorial ...
Could you explain please?
Thank you very much.

All 2 Comments
foxcode

Hi fgb01

That tutorial was made before wade physics was added. Using wade physics would make it easier, but if you are already following that tutorial I will try to explain more clearly.

Firstly, we need to know when the character should jump, in this case, that is determined by 2 factors.
The first is user input. In the tutorial we use a jump button, but it could be a key press or mouse click, whatever you want.
The second is current state. We do not want to allow a jump in mid air, otherwise the character could jump up indefinitely. Instead we only allow a jump when the character is standing on an object. Inside the jump function in the tutorial that's what this first part does.
 

if(this.action == "jumping")
{
    return true;
}
this.action = "jumping";

As you can see, if we are already jumping, we cannot trigger a second jump. However, if we are not jumping, we skip the return statement, and set our current action to jumping.

 

The next 2 lines of code just do some house keeping. We have been keeping a reference to the platform we are currently standing on this.platform. We set this to null because if we are jumping, we are no longer on that platform. We also trigger stop moving. This is actually a bit questionable. Often you would want to simply add an impulse to the y direction, however in the tutorial I'm doing something else.

this.platform = null;
this.stopMoving();

 

I suspect this next line might be what is confusing you. All this line does, is create a velocity that will cause our character to jump in the direction they are currently moving. You can change the sideways speed by increasing the 200 number and you can change the -500 number to change the height of the jump. I've copied the code below, but written it in an easier to understand form.

// This is the code as seen in the tutorial
var impulse = {x:(200*(this.direction=="right" ? 1 : -1)), y:-500};
this.setVelocity(impulse);


// The code below does the same as the code above
var impulse = {x:200, y:-500}; // A velocity, moving up and to the right
if(this.direction == "left")
{
    impulse.x = impulse.x*-1; // If the character is moving left, Reverse the x direction, 
                              // to make the jumping character move left
}
this.setVelocity(impulse);

 

The final piece of code simply plays the correct animation given the direction of the jump. I believe it's pretty self explanatory. If we are moving left, play the jumpLeft animation, otherwise play the jumpRight animation.

if(this.direction == "left")
{
    this.getSprite().playAnimation("jumpLeft");
}
else
{
    this.getSprite().playAnimation("jumpRight");
}

// You may notice that this can be shortened, as I did for the jump velocity
// If you prefer a shorter version, the above code is the same as the following...

this.getSprite().playAnimation(this.direction == "left" ? "jumpLeft" : "jumpRight");

// You can actually shorten it more than that, but lets not get too fancy :)


I hope that helps you understand the jumping part of that tutorial. If you have any issues, please respond to this thread.
 

fgb01

Good Morning,

Is it the best way to use physics?
How would I do that?
I did not understand this method that you used in the tutorial you explained.
Thank you very much for your attention.

Post a reply
Add Attachment
Submit Reply
Login to Reply