Random Numbers
Phaser has built-in methods to generate different types of random numbers (and to select things at random). Using these methods is easier than writing your own code to generate specific types of random numbers based on the JavaScript Math.random()
function.
Random numbers are commonly used in video games to introduce variation that makes the game less predictable. Random numbers are also used to make decisions based on probabilities of certain events happening. Applying random numbers in the right ways can increase the challenge in the game and make repeated playing more enjoyable.
For example, some games place certain resources or obstacles in random or varied locations. Many games use random numbers to create variation in how enemies behave.
Your game doesn't have to use random numbers to be enjoyable, but you should try to think about ways that variation or randomness could improve the gameplay experience.
Phaser coding references in this section:
Get Random Position in Game World
Get Random Member of Group
Get Random Whole Number Within Range
Get Random Decimal Number Within Range
Get Random Angle in Degrees
Get Random Number Between 0 and 1
Get Random Number Between -1 and 1
Get Random Sign (Positive or Negative)
Phaser API Documentation:
Properties and Methods for Random Data Generator
Get Random Position in Game World
You can get a random x
position and/or random y
position within your game world boundaries. For example, this could be used to place resources, obstacles, or enemies within your game.
For example, to add a zombie
to a random position within the game world:
Get Random Member of Group
You can get a random existing member of a group of sprites. For example, this could be used to add unpredictability to enemy behavior.
For example, to select a random member of zombieGroup
to move towards the player
:
Get Random Whole Number Within Range
You can get a random integer (whole number) within a range by specifying the minimum and maximum numbers for the range. (The minimum and maximum values will be included as part of the possible range to randomly select from.)
For example, to give each enemy a random velocity between 125 and 175 pixels per second:
Get Random Decimal Number Within Range
You can get a random real number (decimal number) within a range by specifying the minimum and maximum numbers for the range. (The minimum and maximum values will be included as part of the possible range to randomly select from.)
For example, to give each asteroid
a different size by scaling it to a decimal value between 0.5 and 2:
Get Random Angle in Degrees
You can get a random angle between -180 degrees and 180 degrees.
As a reference for angle direction, here are a few examples:
0
represents pointing to the right-90
represents pointing up-180
and180
both represent pointing to the left90
represents pointing down
For example, in a top-down game, this could be used to give each enemy in a group a random angle for its movement.
Get Random Number Between 0 and 1
You can get a random decimal number between 0 and 1 (which is like getting a random fraction, proportion, or chance).
This is equivalent to using the regular JavaScript random function:
For example, you could use this to make decisions in the game by calculating the chances of something happening. Imagine that your game is designed so that an enemy's bullet has a 25% chance of damaging the player by 10 points — but will otherwise cause only 5 points of damage.
Get Random Number Between -1 and 1
You can get a random decimal number between -1 and 1.
For example, imagine that you want to introduce a small margin of error in the fire angle of the enemy weapon (so the enemy is not a perfect shot every time it aims and fires at the player). Let's say you wanted to add a small random margin of error between -5 and 5 degrees to the enemy's aim.
Get Random Sign (Positive or Negative)
You can get a random sign (positive or negative). It returns a result of either -1 or 1. Think of it like a coin flip: heads or tails.
For example, in a side-view game, this could be used to give each enemy in a group a random direction for its horizontal velocity: about half of the enemies will randomly get a positive velocity (moving to right), and the rest will get a negative velocity (moving to left).
Last updated