0
\$\begingroup\$

i am new to coding and have started a little clicker game and right now im working on a reward system for when the players click amout reaches a certain value. i have figured out how to make one reward but im trying to figure out how to make multiple. I want the value of each click using the click_power var to go up by 2 each time. this is my code so far. '''

The button was clicked times

Click Me! 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;
            }
            document.querySelector('.total-clicks').textContent = clicks;
        }
    </script>
</body>

im trying to use the "reward_received" var using boolean telling if its true or false. every time i try to make a new reward i use reward_received1/2/3 ect.but when i run it, it will only do the reward with the highest required amount. please help and make it make since for a beginner. Thank you for your efforts.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

This is the same as checking for adding a life for a given score.

The key is to change the check value each time it is achieved.

Here are two possible implementations:

Implementation 1

Add another variable var next_reward = 10;

function addClick() {
  clicks = clicks + click_power;
  if(clicks >= next_reward) {
      click_power = click_power + 1;
      next_reward = next_reward + 10; // or any formula your like
  }

Implementation 2

To use an array instead define two variables:

var reward_index =0;
var next_reward = {10,30,500};
function addClick() {
  clicks = clicks + click_power;

// check for end of the array
  if(reward_index < next_reward.length) 
    if(clicks >= next_reward[reward_index]) {
      click_power = click_power + 1;
      reward_index = reward_index + 1;
    }
    // optional
    else {
      reward_index = next_reward.length -1; //set last element to ...
      next_reward[next_reward.length -1] = next_reward[next_reward.length -1] * 100; // or any formula your like
    }     

Exclude the else if no rewards are needed after the array is exhausted.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .