[ad_1]
If you want to reward the player with additional click power, then it would make sense to make that a separate variable, and use that variable instead of the constant value of 1
when you increase the click count:
let clicks = 0;
let click_power = 1;
function addClick() {
clicks = clicks + click_power;
Now your game should still behave as before. But by turning a constant into a variable we created the possibility for the player’s click power to change over the course of the game.
Now how do we increase the click power? Well, whenever we increase the click count of the player, we need to have some conditional code that only executes if the click count reached a certain level. We can do that with the if
statement.
function addClick() {
clicks = clicks + click_power;
if(clicks == 10) {
click_power = click_power + 1;
}
This simple solution should increase the players click power by 1 the moment they reach 10 clicks. This works for now, but could lead to problems down the road. What if we add more than one way to increase the click power? And what if the player does that before reaching the 10 click threshold? In that case it is possible that clicks
jumps from 9
right to 11
. It would never be exactly equals to 10
. So the reward for reaching 10 would get skipped. How do we solve that?
Well we could try to increase it when the player reached or exceeded 10 clicks:
if(clicks >= 10) {
click_power = click_power + 1;
}
But this causes another problem. Now the player gets the reward every time they click the button after 10 clicks. We have to somehow memorize that the player already received the reward. We can do that with a 3rd variable that keeps track of whether or not the player received the reward and only give it to the player when he hasn’t received it yet:
let clicks = 0;
let click_power = 1;
let reward_received = false;
function addClick() {
clicks = clicks + click_power;
if(clicks >= 10 && reward_received == false) {
click_power = click_power + 1;
reward_received = true;
}
Now the player only receives the reward if two conditions are true: They reached or exceeded 10 clicks and they didn’t receive the reward yet.
Note that if you decide to add more rewards with different conditions, then you are probably going to need a separate variable for each reward. That means you could end up with quite a lot of variables and conditions. There are ways to structure that in a better way. But that’s a more advanced topic for another question.
[ad_2]