Want to know how to make a ball roll on hills in Hopscotch? Well, I've decided to make a tutorial on it! Of course, you don't have to use a ball. :D
Anyway, let's start by making the hills.
(I'm going to use the hills shown above, but feel free to use different hills or modify the code of these ones!
To make the hills shown in the picture above, we're going to have to use the sine block. It's actually rather easy to make these hills, just use this code:
As I said before, feel free to modify the code! :D
You may want to copy the code in the y section of the Set Position block. It'll come in useful later. (You don't have to copy it, but I would recommend it.)
Now to make the ball!
I'm going to make the ball a Circle object, but you can make it whatever you want.
Now to make it possible for the ball to move! I'm going to make it controlled by tilting the iPad.
This code makes the ball constantly change its x position based on how much the iPad is tilted right.
Now, we'll need to make the ball actually roll on the hills. To do this, we'll need to make a few new rules. One of the new rules will make the ball look like it's being pulled down by gravity, and the other will make it bounce when it hits the ground.
The first new rule will be a "When game starts" rule. It will only need a few blocks in it, just copy the code below and put it in the rule.
(you'll need to make a value)
If you play your project now, the ball should fall until it hits the bottom of the screen.
Now for the second new rule! This rule will be a "When __ < __" rule.
Replace the first blank with the ball's y position, and when you get to the second blank (assuming you copied the thing I said to copy), just press paste! If you didn't copy it, it's fine, just put in the code (
). Just remember to replace the x position of the object that draws the hills with the x position of the ball!
If you modified the code for the hills, be sure to put the modified version of instead, or it won't work.
Anyway, now that you've made the actual rule, let's actually put some code in it. :D
To make the ball bounce when it hits the ground, we'll only need 2 blocks. Here they are:
However, when you play the project, something seems to be wrong; the ball should be on top of the line, not in the middle! Fortunately, this is easy to fix.
To fix it, you'll need to add a certain number to the things circled in the picture below.
To figure out what number to add, you might have to mess around with it a bit, but once you find the number, the ball will be able to roll and bounce around on the hills! :D
Feel free to mess around with the code! :D
If you want, you can also add stuff, like making the ball "jump" when you tap the iPad, for example. You could even turn this into a game! :D