let dpc;
let money;
if (getCookie("money") == -1) {
money = 0;
} else {
money = +getCookie("money");
$("#money").html(money);
};
if (getCookie("dpc") == -1) {
dpc = 1;
} else {
dpc = +getCookie("dpc");
};
$(".dpc").click(function() {
let thisCost = +$(this).find("span[id=dpcCost]").text();
let thisBuff = +$(this).find("span[id=dpcBuff]").text();
if (money < thisCost) {
alert("not enough money")
} else {
dpc += thisBuff;
money -= thisCost;
};
});
let enemyNum = 0;
let currentEnemy = $(".enemy").eq(enemyNum);
let K = 100;
let health = currentEnemy.attr("data-health");
let k = K / health * dpc;
let MAX_HEALTH = health;
enemy.click(function(e) {
if (currentHealth.html() != 0) {
K -= k;
health -= dpc;
changeHealthBar();
changeHealth();
$(".minus-hp").remove();
$(this).append(`<span class="minus-hp">-${dpc}</span>`)
let posX = e.offsetX,
posY = e.offsetY;
$(".minus-hp").css({"top": posY,"left": posX});
};
if (currentHealth.html() == 0) {
currentEnemy.addClass("defeat");
setTimeout(removeOldEnemy, 1000);
};
if (currentHealth.html() < 0) {
currentEnemy.addClass("defeat");
setTimeout(removeOldEnemy, 1000);
currentHealth.html(0);
};
});我有这段代码,问题是当我点击$(". dpc")时,我更改了一些变量,但是在单击的另一个位置(enemy.click(函数(e)),变量的原始值被存储,如何使它成为当我单击$(". dpc")时,变量在enemy.click(函数(e) )中发生变化?
发布于 2021-01-22 23:40:52
从贴出来的..。$(".dpc").click上唯一的变量更改是dpc和money。
现在,在ennemy.click函数中,可能需要更新的唯一变量是k。
因此,将K -= k;更改为K -= K / health * dpc;
你可能需要把它放在health -= dpc;之后
https://stackoverflow.com/questions/65853740
复制相似问题