Randomness

Let’s introduce some uncertainty into our creation.

We can use uw.randomInt(a, b) to pick a random number between a and b. (This is just like pick random _ to _ from Scratch.)

For example, uw.randomInt(1, 10) gives a random number between 1 and 10.

Random position

Let’s start our sprite off in a random place on the screen.

👉 Set your sprite to a random X position.

player.posX = uw.randomInt(0, world.width)

👉 Set your sprite to a random Y position.

Hint: you’ll need to add another line, which uses world.height.

👉 Check that each time you refresh, the sprite moves to a different place on the screen!

Random angle

Let’s point our sprite in a random direction.

player.angle = uw.randomInt(1, 360)

👉 Check that each time you refresh, the sprite points in a different direction.


➡ Next, let’s learn how to animate things