Discuss Scratch

codemaster14444
Scratcher
5 posts

How to Make Gravity on Scratch for Beginners

1. Make a new sprite, which will be used as the ground and call it ‘platforms’. Make sure it is at coordinates 0, 0 and the sprite in the costume tab is put in the place you want. Also, name the cat, or the sprite you designed for the player, as player.

2. Add the following blocks to this script that makes gravity on the sprite that is used for the player.

1. Add a when green flag clicked block from the events category.
2. Add a go to x: (0) y: (0) block from the motion category.
2. Create a variable called yVelocity or yV for short and make sure it's for this sprite only.
3. Add a set to block from the variables category.
4. Add a repeat until loop from the controls category.
5. Inside the repeat until loop, put a <touching platforms?> boolean from the sensing category inside the black slot.
6. Add a change by (-1) block from the variables category, inside the repeat until loop.
7. Still inside the repeat until loop, add a change y by (10) block under the change by (-1) block.
8. Get a yV input variable from the variables category and put it inside the slot in the change y by (10) block.

3. If you created a square for the player, you may have spotted a bug where the sprite actually lands inside the platform. This is actually normal, so don't worry, there's always a way to fix it. This is because scratch thinks touching another sprite or colour means that the sprite needs to be completely wedged inside the sprite it's supposed to touch. Add the following blocks to this script that fixes this bug. Make sure you are still in the player sprite.

1. Go to the my blocks category and make a custom block called and check the ‘run without screen refresh’.
2. Add an <if> block from the controls category under the new custom block.
3. Inside the slot in the if block, add a <touching platforms?> boolean.
4. Inside the if, add a repeat until block from the controls category.
5. In the slot in the repeat until block, add a <not <>> from the operators category. Put a <touching platforms?> boolean from the sensing category in the slot for the <not <>> boolean.
6. In the repeat until loop, add a change y by (1) block from the motion category.

4. Finally, we need to activate the custom block . Add the following blocks to this script that activates the new block. You need to still be on the player sprite.

1. Add another when green flag clicked block from the events category.
2. Under the previous block we added, put a forever loop from the controls category.
3. Inside the forever loop, insert the new Fix Collisions block from the my blocks category.

5. Well done! You are now finished! You have made a succesful piece of code that works with gravity using my help. Well done!
<Disclaimer>
If there are anymore bugs that I haven't talked about, please let me know.

Powered by DjangoBB