Case Study
Recharge Rate Up
Add a new type of PowerUp, which makes the ships
energy recharge faster.
In the current game, you have the energy recharge based off an
on game update interval event, which occurs every 750ms.
It may seem like this is an easy change, by just using a variable
for the interval instead of a specific time, but this doesn’t quite work.
The game.onUpdateInterval function takes in an interval in milliseconds
and an event handler function, and causes the event handler to occur on the interval
To fix this, you will need to change the on game update interval
event in the ship
namespace to an on game update event,
and keep track of the time yourself. The game.runtime function
is useful for this, as it gives the time since the game game was started,
in milliseconds.
First, create two new variables in the ship
namespace:
rechargeDelay and lastRecharge.
rechargeDelay should keep track of the current delay between recharges,
and should start at 750ms (like the previous on game update interval).
lastRecharge should keep track of the last time that the ship’s
currentCharge
was incremented.
In the on game update event, get the current time,
and check if the time that has passed since the lastRecharge
is greater than or equal to the rechargeDelay.
If it is, update lastRecharge to the current time,
and increment currentCharge if it is less than maxCharge.
Finally, add the new RechargeRateUp
PowerUp
to the game.
In the overlap event between Player
and PowerUp
,
decrement the ship.rechargeDelay by 20 if the PowerUp
is of type RechargeRateUp
.
Set the response
for this PowerUp
to “Faster Charge!”.
Solution
enum PowerUpType {
Health,
Score,
EnergyUp,
RechargeRateUp
}
namespace ship {
export let rechargeDelay = 750;
let lastRecharge = 0;
game.onUpdate(function () {
let currentTime = game.runtime();
if (currentTime - lastRecharge >= rechargeDelay) {
lastRecharge = currentTime;
if (currentCharge < maxCharge) {
currentCharge++;
}
}
});
}
namespace powerups {
let availablePowerUps = [
PowerUpType.Health,
PowerUpType.Score,
PowerUpType.EnergyUp,
PowerUpType.RechargeRateUp
];
export let responses: string[] = [];
responses[PowerUpType.Health] = "Got health!";
responses[PowerUpType.Score] = "Score!";
responses[PowerUpType.EnergyUp] = "More Energy!";
responses[PowerUpType.RechargeRateUp] = "Faster Charge!";
}
namespace overlapevents {
sprites.onOverlap(SpriteKind.Player, SpriteKind.PowerUp, function (sprite: Sprite, otherSprite: Sprite) {
let powerUp: number = powerups.getType(otherSprite);
otherSprite.destroy();
sprite.say(powerups.responses[powerUp], 500);
if (powerUp == PowerUpType.Health) {
info.changeLifeBy(1);
} else if (powerUp == PowerUpType.Score) {
info.changeScoreBy(15);
} else if (powerUp == PowerUpType.EnergyUp) {
ship.maxCharge++;
} else if (powerUp == PowerUpType.RechargeRateUp) {
ship.rechargeDelay -= 20;
}
});
}