In the update calls, we have been incrementing the position of the ship sprite by 2. In real life, however, you may want to store that value as a global constant or a centralized setting. In this case, changing the overall speed means that we need to update a single constant or variable, instead of refactoring the whole game.
We have already moved the screen size into constants; let's do the same with the speed:
- Introduce a new constant called shipSpeed and set its value to 2:
const screenWidth = 800;
const screenHeight = 600;
const shipSpeed = 2;
- Now, update all of the existing code and use the shipSpeed constant in all of the places you need to increment or decrement the position of the ship, as shown in the following code:
function update() {
// RIGHT button
if (cursors.right.isDown) {
ship.x +=...