The game is about bouncing on platforms to get higher and higher…forever. Your score is based on how high you can jump.
The first thing to do is to make our player fall down.
We can simulate gravity using a constant acceleration. As you should know from Maths or Physics class, that means we need to keep track of velocity (speed and direction). Let’s add a variable for just that.
Add a variable called velY
, which stores a number.
var velY = -2
Add a forever
loop for the player.
player.forever(() => {
player.posY += velY
})
Save. You’ll notice the player falls at a constant speed, which isn’t right at all! Let’s change velocity over time, to simulate gravity:
Decrease velocity, so the player falls.
var velY = -2
var velY = 0
player.forever(() => { // player
player.posY += velY
velY -= 2 // <-- add this line
})
Now our player falls down! Let’s add some platforms for him to bounce on. (We won’t do the bouncing part just yet; that’ll have to wait for later.)
Create a platform
. This is going to be a Polygon
instead of a Sprite
.
var platform = new Polygon
platform.points = [[-30, 0], [30, 0], [30, 10], [-30, 10]]
platform.posY = 100
Set the fill
and outline
colors for your Polygon, to taste.
Tweak the size of your platform as necessary. You can do this by changing its points
.
Save. Check the platform appears below the player!
platform.closed
.