CarryNeo
CarryNeo

Reputation: 33

Automatically reset value on the fly

I try to reset a value when it goes to 0. so I added this on top of my code but it doesn't seems to work :

if (gameData.monster_hp <= 0) {
  gameData.monster_hp = 100;
}

And here here my whole code :

var gameData = {
  player_hp: 0,
  monster_hp: 100,
  hit_dmg: 5,
  atk_speed_ratio: 4,
}


if (gameData.monster_hp <= 0) {
  gameData.monster_hp = 100;
}


function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

function hit() {
  gameData.monster_hp -= gameData.hit_dmg
  document.getElementById("monster_hp").innerHTML = gameData.monster_hp + "HP"
}
async function auto_hit() {
  while (gameData.monster_hp > 0) {
    hit();
    s = 1000 / gameData.atk_speed_ratio,
      await sleep(s);
  }
}

function dmg_up(n) {
  gameData.hit_dmg = gameData.hit_dmg + n;
  console.log(gameData.hit_dmg);
}

function atkspeed_up(n) {
  gameData.atk_speed_ratio = gameData.atk_speed_ratio + n;
  console.log(gameData.atk_speed_ratio);
}

Upvotes: 2

Views: 61

Answers (2)

Kiran Shinde
Kiran Shinde

Reputation: 5982

Its very hard to understand your problem from this code. You should add a runnable snippet which will demonstrate your problem but by looking at your code, you can put your check in hit function

Like this

function hit() {
  gameData.monster_hp -= gameData.hit_dmg
  document.getElementById("monster_hp").innerHTML = gameData.monster_hp + "HP"
  if (gameData.monster_hp <= 0) {
     gameData.monster_hp = 100;
  }
}

Upvotes: 0

Safwan Samsudeen
Safwan Samsudeen

Reputation: 1707

You can put

if (gameData.monster_hp <= 0) {
  gameData.monster_hp = 100;
}

inside a function named checkScore. Then, whenever you make a change to gameData.monster_hp, you can call the function checkScore.

Upvotes: 1

Related Questions