How do I make the platformer's jump height vary depending on the tap or click duration? Think Mario and Sonic, those dudes knew how to jump high and low!
Don't Panic
I see this topic come up a lot in the forums, and I have seen a multitude of different ways people have described what they are looking for (can haz jump length happy move time science magic?) and how to solve the problem. Here I present the way that has worked best for me.
Define the Problem
So the problem is, in closer to software engineering speak ->
The platform character shall continue to jump upwards, not past the max jump height, until the user lets go of the jump button, causing the jump height to be reduced.
I have seen people pulling this off with timers that count how long the user holds in the jump button in relation to the max amount of time the user is allowed to hold the jump button in; then take the difference and apply the percent of the max hold time to the max jump height....... my head hurts already. This isn't a bad way to solve the problem; I just think it forces you to use a lot of unnecessary variables.
I have also seen people try to do this by altering the gravity, which is not a great way to solve the problem in my opinion because it doesn't look as natural.
Learn Some Physics
So on to a very brief explanation of what C2 is doing in the background. But first, homework. We are going to solve this problem by manipulating the object's upward velocity, so take a minute to read over this.
Physics Lesson - Acceleration
So what does all that matheblackmagics mean? In short, acceleration is a vector that describes the rate at which an object's velocity is changing. If you have driven a car you should have an understanding of acceleration and deceleration. But what is this vector business?
A vector is a set of numbers like this [1, 42, 1984] - a lot like an array in C2 (and programming languages). The cool thing about acceleration is that it's not just one number but a set of numbers, that are describing changes in velocity on each plane X,Y, and sometimes Z. In C2, these are 2D vectors [X,Y]. In 3D engines, an acceleration vector would have an additional value for changes in "Z velocity" [X,Y,Z].
When C2 makes your platformer character jump, there isn't something hard coded in there to move it a certain amount of pixels by hand the of C2 gods, but rather a physics engine that calculates where an object should be on the screen based on the forces that have been applied to an object and the amount of time that has passed. The forces applied are acceleration vectors. Each time you add a new force, you add a new acceleration vector to the object's current velocity vector.
Think of it as something hitting the platformer from the bottom to make it jump. You are hitting it with an acceleration vector by adding the values for each plane from the upwards force being applied to the object's acceleration vector. All the while another force is being applied, gravity, which eventually takes over and pulls the character back down. Using these physics, we can manipulate the Y acceleration of the platformer to make the character stop jumping when the jump button is released.
Implementation in Construct 2
So let's get our hands dirty. I have altered and attached the AutoRunner template that is presently included with C2.
So our desired functionality was: The platform character shall continue to jump upwards, not past the max jump height, until the user lets go of the jump button causing the jump height to be reduced. We can determine that the player has let go of the jump button if the platformer is moving upwards and the button is not currently being pressed. If this event condition is true we can then change the upward acceleration value of the platformer. Here is what it looks like in C2.
By cutting the Y value of the platformer's velocity vector in half, the platformer will decelerate on the Y plane causing gravity to take over sooner; It's just like a car driving up a hill, if you let off the accelerator, you will eventually roll backwards. Reducing the Y vector will cause the jump to be shorter because you are letting off the Y accelerator. I achieve this here by halving the Y vector value each time the event fires. You could make the process more or less aggressive by making a bigger change in the Y vector (Y/4) (will make it look like its hitting an invisible wall), or by just cutting a constant from it each time (Y= Y-1). The "Is Jumping" condition will only be true when the platformer is moving upwards, so once the platformer starts to fall, no changes are made to the Y vector value. By holding the button in the whole time the platformer is moving upward, the platformer will jump as high as it can before gravity takes over, but letting go reduces upward acceleration. Reducing the Y velocity is essentially the same as applying a a deceleration vector to the object's velocity.
I hope this was helpful to you!