📋
Adding gravity is what turns a basic project into a platformer. To make gravity look "real," we can't just move the sprite down at a constant speed. Instead, we need the sprite to accelerate (get faster as it falls).
To do this, we use a Variable.
Step 1: Create the "Y-Velocity" Variable
A variable is like a container that holds a number. We will use it to track how fast the sprite is currently falling.
Go to the Variables category (orange).
Click "Make a Variable" and name it
y-velocity.
Step 2: The Core Gravity Script
This script does two things: it pulls the sprite down and stops it when it hits the ground.
The Logic:
When 🚩 clickedset [y-velocity] to 0(Reset speed at start)foreverchange [y-velocity] by (-1)— This is the "Gravity" constant. It makes the downward speed faster every second.change y by (y-velocity)— This actually moves the sprite.if <touching [Ground Sprite or Color?]> thenset [y-velocity] to 0— This stops the falling.repeat until <not <touching [Ground]?>>change y by (1)— This prevents the sprite from getting stuck inside the floor.
Step 3: Adding the Jump
Now that gravity is pulling you down, you need a way to go up!
Inside your forever loop (and inside the if touching ground block), add this:
if <key [space] pressed?> thenset [y-velocity] to 15
Why this works:
Because you set the y-velocity to a positive number ($15$), the sprite will suddenly shoot upward. Then, because the gravity script is always running, it will slowly subtract $1$ from that $15$ until it becomes $0$ (the peak of the jump) and then becomes negative (falling back down).
Summary of the Physics
Gravity:
change [y-velocity] by (-1)Movement:
change y by (y-velocity)Ground:
set [y-velocity] to 0Jumping:
set [y-velocity] to 15
Tip: If you want "Moon Gravity," change the
-1to-0.2. If you want "Jupiter Gravity," change it to-5!
Would you like me to explain how to make a "Scrolling Background" so your character can travel through a long level?
📋
0 件のコメント:
コメントを投稿