在jQuery中的Exp计

时间:2016-10-13 23:54:13

标签: jquery

我有这个功能。它很好地填充了我的exp计,但是当exp大于剩余的exp到下一个级别时,它只会缩小到零。我想要的是让仪表填满100%,然后继续填补下一级所获得的剩余金额。 我希望我在这里清楚。否则告诉我,我会尝试更好地解释它。

缺少什么功能?

//These are my variables
var currentXP = 0;
var level = 1;
var maxXP = 250;

//This is my function
function Grow() {
    if(currentXP <= maxXP){
            $('#active_meter').animate({height:currentXP + "px"}, 500);
        }else{
            currentXP = maxXP - currentXP;
            level++;
            console.log("Level: " + level);
            $('#active_meter').animate({height:currentXP + "px"}, 500);
            document.getElementById("level").innerHTML = level;
    }
  }

1 个答案:

答案 0 :(得分:1)

当体验超过最大值时,将其设置为最大值并设置currentXP to the amount left over in the new level. In the completion function for that animation, set the height down to 0`并将其设置为新的exp级别。

//These are my variables
var currentXP = 0;
var level = 1;
var maxXP = 250;

//This is my function
function Grow() {
  if (currentXP <= maxXP) {
    $('#active_meter').animate({
      height: currentXP + "px"
    }, 500);
  } else {
    currentXP = currentXP - maxXP;
    level++;
    console.log("Level: " + level);
    $('#active_meter').animate({
      height: maxXP + "px"
    }, 500, function() {
      $('#active_meter').css('height', '0px').animate({
        height: currentXP + "px"
      }, 500);
    });
    document.getElementById("level").innerHTML = level;
  }
}