Jumping vs. Flying: Catching Air in MakeCode Arcade.
Jumping is a common mechanic in video games, but when sending your character into the air, there’s more to consider than whether or not it will come back down.
Learn to Fly
In order for your sprite to take flight, it needs to have some type of upward movement. This is usually achieved by setting its vertical velocity (vy) — also known as the speed that it’s traveling in the up/down direction.
If you set the sprite’s vertical velocity to a negative number (and there’s no acceleration or friction to get in the way) your sprite will take off into the sky and never come back down!
Some games, like Flappy Bird, rely on a gravity mechanic that you must overcome with each push of the button. Every time you push your space bar, your bird goes higher into the air, but gravity will pull you back to the ground when you stop.
A more extreme version of this is to check to see if your character is already on the ground before allowing it to take off again. This is less like flying, and more like jumping.
Preparing for the Jump
Setting a positive acceleration in your game will create a consistent “pull” toward the bottom of the screen. If you then press the (A) button to inflict negative velocity, you’ll temporarily head in an upward direction, until the acceleration overwhelms the velocity and you start dropping again.
The trick to ensuring complete jumps is to make sure that the action inside of the (A) button won’t run again until the sprite is back on the ground. You can do that with a simple
In our Jungle Jump skillmap, we call this a gravity jump.
The code above works perfectly well for tilemap situations, but it doesn’t do a great job at handling sprite jumps in non-tilemap games.
To account for situations where only a background is used and the character may or may not travel beyond the edge of the screen, some additions were made to our special
gravity jump extension.
Let’s break this down condition by condition.
- Is mySprite hitting a wall at the bottom?
If so, you’re on the ground. Go for it!
- Is the velocity of mySprite = 0?
If so, you’re not flying or falling. Go for it!
- Is the bottom of your sprite at the bottom of the screen?
If so, you can’t have been traveling upward already. Go for it!
If any one of the above cases is true, this code will allow your character to jump again.
And now you have all the information you need to code your own jump mechanic! What kind of game will you add this to?