Intro #2: Animation

Animation

So far, we’ve only set up static scenes. We’ve learnt how to position different kinds of objects on the screen, and change how they look; but we haven’t moved them or changed them after initialisation.

Animation means to give an appearance of movement. We do this by changing values over time.

👉 Save, and check your Sprite appears!

Forever

We need a way to react to time passing. We do this by using forever to run code on every frame.

An app draws a new frame on the screen roughly 60 times a second (or 60 FPS, frames per second).

We can use forever to make our sprite spin!

The forever blocks are fiddly to type; sorry about that. Make sure you get the brackets exactly right!

In JavaScript, n += 1 is shorthand for n = n + 1. You can read it as “change by”.

We’re increasing the angle of the sprite a little bit on every frame, so our sprite will slowly rotate clockwise.

👉 Save, and you should see your Sprite start to spin!

👉 Make sure you can get your Sprite to spin the other way.

Now we know the basics of animation, lets try some simple movement.

👉 Save, and the Sprite should move to the right instead of rotating.

The numbers inside the forever are how much to move on each frame, so they control how fast the animation happens.

👉 Make sure you can get your Sprite to move faster.

Note that multiplying the scale by the fraction 1.05 is the same as increasing it by 5%.

👉 See if you can get your Sprite to get bigger.

Events

Let’s have our face shoot out a projectile toward our finger when we tap.

👉 Make sure your sprite stops moving.

The code inside the on(... block runs whenever the screen is tapped. So whenever you tap (or click) the screen, the message “don’t touch this” should appear.

👉 Save, and check that the message appears when you tap the screen.

That gets boring quickly, so let’s make a projectile.

👉 Save, and check that a bullet appears wherever you tap.

What’s going on here? e is an event object, telling us the details of the tap event. The e.fingerX and e.fingerY attributes tell us the X & Y coordinates where the tap happened.

Now let’s try moving our projectile!

Notice that the forever block has to be inside the world.onTap block. Just as we can’t use a name like cow before it’s been created, we can’t use the name bullet outside the block that it was created inside. This phenomenon is called “scope”.

👉 Save, and check that a bullet appears and starts moving, wherever you tap.

Moving at an angle

Sometimes we want to move things at an angle – and to do this, we need trigonometry!

It’d be really neat if our bullets started at the face, and travelled outward toward the point where we tapped. Then it would feel more like a “cannon” sort-of game.

👉 Make sure the bullets always start from the face.

To move our bullets, we only need to know two things from trigonometry:

  1. You can use .posX += uw.sin(angle) and .posY += uw.cos(angle) to move a sprite at an angle.

  2. You can use angle = uw.atan2(dx, dy) to work out the angle you need to move something in a direction.

Currently, our bullets all go at 45°, which is dull. Let’s fire them out at a random angle.

👉 Check the bullets are created pointing in a random direction.

👉 Check the bullets move in the same direction as they’re facing.

By multiplying sin and cos by 4, we increase the speed of movement.

So we’ve done the first part – moving the bullets in a direction.

Now to work out the correct angle! We need to use atan2 for this. This is a special version of inverse tan() which takes two numbers – a difference in X and a difference in Y – and returns the correct angle.

We’ll call the difference in X dx, and the difference in Y will be dy.

👉 Check the bullets move toward your mouse.

Congrats! We’ve just made a… face-cannon… thing.

Conditions

Animation is all very well, and we’ve learnt how to react to events. But how can we react to other things changing?

A condition returns a Boolean value: either true or false. They look like this:

  1. x === y means x is equal to y

  2. x !== y means x is not equal to y

  3. x < y means x is less than y

  4. x <= y means x is less than or equal to y

  5. !p means nottrue if p is false, and vice-versa

  6. p && q means andtrue only if both p and q are true

  7. p || q means ortrue if either of p or q is true

Here are some examples. These are all true:

  1. 1 !== 2
  2. 42 === 42
  3. "moo" === "moo"
  4. 10 < 42
  5. !(false)
  6. true && true
  7. false || true

Finally, there’s a really important condition built-in to you-win, called .isTouching(otherSprite). More about that later!

Let’s see what we can do with conditions.

This time, we’re giving its speed a name: xVel. We’re doing this so we can change it later.

Just as you can give a name to Sprites, you can give a number a name to “remember” it for later. You can change it later, too.

Currently, our face moves to the right (increasing X).

We do this by comparing his right edge to the width of the world. If his right edge is greater than the width of the world, then he’s gone off the right-hand side of the screen; so we set his velocity negative, so he starts moving left instead.

Fin

Excellent work! You’ve learnt how to:

What next?

If you want something else to try, here are some extensions to try! Why not: