我正在try 做一个非常基本的RPG游戏,当你攻击时,经验条将减少20px的css宽度. 现在我已经完成了,一切似乎都运行得很好,但是当我更快地点击攻击按钮时,我注意到了一个错误.在某个点上,EXP值不会减少20px.它可能只会减少10px,甚至可能是3px.我不确定我的数学实现是错误的,还是我还没有找出的错误.
以下是我的Java脚本代码:
const atkBtn = document.getElementById("atk-btn");
let expBar = document.getElementById("bar");
function attack(){
var barWidth = expBar.offsetWidth;
var atkVal = 20;
newBar = (barWidth - atkVal)+"px";
expBar.style.maxWidth = newBar;
console.log(newBar)
}
atkBtn.addEventListener("click", attack);
以下是我的css代码:
p{
margin: 0px;
display: inline-block;
}
#bar{
width: 200px;
height: 15px;
margin-top: 10px;
background-color: red;
display: inline-block;
max-width: 200px;
overflow-x: hidden;
transition: all 0.4s;
}
Here is when I clicked the btn slowly:
Here is when I clicked the btn faster(to simulate gaming condition):
如果您有任何答案,请随时回复.谢谢