Нужно отнять значения числа через сет интервал
Есть значения здоровья которые нужно отнимать со скоростью в зависимости от скорости атаки персонажа. Проблема: здоровье не отниаеться при console.log(curentChartherHealth) выводиться стартовое здоровье персонажа
let curentChartherHealth
let curentRandHealth
let timerId = setInterval(()=> {
curentChartherHealth = character.health - rand.damage-character.armor
console.log(curentChartherHealth)
},character.atackSpeed)
let timerId2 = setInterval(()=>{
curentRandHealth = rand.health - (character.damage-rand.armor)
},rand.atackSpeed)
if(curentChartherHealth<=0){
alert(`${rand.name} Win!`)
clearInterval(timerId)
} else if(curentRandHealth<=0){
alert(`${character.name} Win!`)
clearInterval(timerId)
}
}
Ответы (1 шт):
Автор решения: Daniil Loban
→ Ссылка
Если кратко то Вы не меняли character.health а в этом случае использование его в любой формуле будет давать всегда один результат. Я немного упростил код и так же вынес проверку в отдельную функцию (на мой взгляд так лучше) Обратите внимание на -= так я меняю значение а не просто использую его в расчетах.
character = {
health: 100,
}
rand = {
health: 100
}
let curentChartherHealth
let curentRandHealth
const check = () => {
if(curentChartherHealth<=0){
console.log(`rand Win!`)
clearInterval(timerId)
} else if(curentRandHealth<=0){
console.log(`character Win!`)
clearInterval(timerId)
}
}
let timerId = setInterval(()=> {
curentChartherHealth = character.health -= 1
console.log(curentChartherHealth)
check()
},100)
let timerId2 = setInterval(()=>{
curentRandHealth = rand.health -= 1
check()
},100)