I'm currently learning JavaScript, and I wrote some codes that work just using basic functions, but now I'm trying to convert my codes into a Class and I got everything working except 1 thing. I'm trying to call a function from within a function in a class:
class Donut {
constructor(
donutCountSpan,
autoClickerPriceSpan,
autoClickerCountSpan,
donutMultiplierCountSpan,
donutMultiplierPriceSpan
) {
this.donutCountSpan = donutCountSpan;
this.autoClickerPriceSpan = autoClickerPriceSpan;
this.autoClickerCountSpan = autoClickerCountSpan;
this.donutMultiplierCountSpan = donutMultiplierCountSpan;
this.donutMultiplierPriceSpan = donutMultiplierPriceSpan;
this.donutCount = 200;
this.donutMultiplierCount = 0;
this.autoClickerCount = 0;
this.autoClickerPrice = 100;
this.donutMultiplierCount = 0;
this.donutMultiplierPrice = 10;
}
activateAutoClickers() {
setInterval(function () {
this.donutCount +=
this.autoClickerCount * Math.pow(1.2, this.donutMultiplierCount);
this.donutCountSpan.innerText = Math.round(this.donutCount);
}, 1000);
}
buyAutoClicker() {
if (this.donutCount >= this.autoClickerPrice) {
this.donutCount -= this.autoClickerPrice;
this.autoClickerCount += 1;
this.autoClickerPrice = Math.round(this.autoClickerPrice * 1.1);
this.donutCountSpan.innerText = Math.round(this.donutCount);
this.autoClickerPriceSpan.innerText = this.autoClickerPrice;
this.autoClickerCountSpan.innerText = this.autoClickerCount;
if (this.autoClickerCount <= 1) {
this.activateAutoClickers(); //THIS IS THE METHOD THAT ISN'T BEING CALLED
}
}
}
Here are the event listeners for my buttons:
donut.addEventListener("click", () => {
donutClicker.addDonut();
});
buyAutoClickerButton.addEventListener("click", ()=> {
donutClicker.buyAutoClicker();
});
buyDonutMultiplierButton.addEventListener("click", ()=> {
donutClicker.buyDonutMultiplier();
});