See:

-WORKING EXAMPLE-

How to replicate my *problem:

If you put all your bonus points into a stat except 1, and then CTRL-CLICK in the other stat, then the bonus value will go into the negative, when *it should be stopping at 0.

Controls:

  • Left-Click = Allocate 1 point
  • CTRL Left-Click = Allocate 5 points
  • Right-Click = Take back 1 point
  • CTRL Right-Click = Take back 5 points

JavaScript:

var Alexander =
    {
      Magic: "MagicVal",
      Attack: "AttackVal",
      Bonus: "BonusVal",
      Limits: {
        Magic:  {
          max: 100,
          min: 80
        },
        Attack: {
          max: 100,
          min: 80
        }
        }
    };

function table(e)
{
  e.preventDefault();
}

function add(character, stat) //Allocates "1" with left-click//
{
  var txtNumber = document.getElementById(character[stat]);
  var newNumber = parseInt(txtNumber.value) + 1;
  if(newNumber > character.Limits[stat].max) return;
  var BonusVal = document.getElementById(character.Bonus);
  if(BonusVal.value <= 0) return;
  var newBonus = parseInt(BonusVal.value) - 1;
  BonusVal.value = newBonus; 
  txtNumber.value = newNumber;
}

function subtract(character, stat) //Takes back "1" with right-click//
{
  var txtNumber = document.getElementById(character[stat]);
  var newNumber = parseInt(txtNumber.value) - 1;
  if(newNumber < character.Limits[stat].min) return;
  var BonusVal = document.getElementById(character.Bonus);
  var newBonus = parseInt(BonusVal.value) + 1;
  BonusVal.value = newBonus; 
  txtNumber.value = newNumber;
}

function dump(e, character, stat) //Allocates "5" with CTRL+left-click//
{
  if (e.ctrlKey == 1 && e.which == 1) {
  console.log('met');
  var txtNumber = document.getElementById(character[stat]);
  var newNumber = parseInt(txtNumber.value) + 4;
  if(newNumber > character.Limits[stat].max) return;
  var BonusVal = document.getElementById(character.Bonus);
  if(BonusVal.value <= 0) return;
  var newBonus = parseInt(BonusVal.value) - 4;
  BonusVal.value = newBonus; 
  txtNumber.value = newNumber;
}
}

function reclaim(e, character, stat) //Takes back "5" with CTRL+right-click//
{
  if (e.ctrlKey == 1 && e.which == 3) {
  console.log('met');
  var txtNumber = document.getElementById(character[stat]);
  var newNumber = parseInt(txtNumber.value) - 4;
  if(newNumber < character.Limits[stat].min) return;
    var BonusVal = document.getElementById(character.Bonus);
  var newBonus = parseInt(BonusVal.value) + 4;
  BonusVal.value = newBonus; 
  txtNumber.value = newNumber;
}
}
有帮助吗?

解决方案

you have to replace ( in your dump method) this

  if(BonusVal.value <= 0) return;
  var newBonus = parseInt(BonusVal.value) - 4;

with this :

  var newBonus = parseInt(BonusVal.value) - 4;
  if(newBonus <= 0) return;

其他提示

It seems you are checking the existing value for Bonus before perfoming any adjustment to it. Thus if the bonus were say 1 and you were trying to remove 5 from it, the check would show value at 1 and proceed with the math to change the value to -4.

My guess is that you will want to apply the math, and then check against 0 afterwards and adjust to 0 if value < 0.

the working example!...

http://jsbin.com/omigOti/1/edit

you need verify if the bonus will be negative... like this correction...

function add(character, stat) //Allocates "1" with left-click//
{
  var txtNumber = document.getElementById(character[stat]);
  var newNumber = parseInt(txtNumber.value) + 1;
  if(newNumber > character.Limits[stat].max) return;
  var BonusVal = document.getElementById(character.Bonus);
  if(BonusVal.value <= 0) return;
  var newBonus = parseInt(BonusVal.value) - 1;
  if( newBonus < 0 ){ //try if is negative

    newBonus = 0; //and fix it
    newNumber = +(BonusVal.value);

  } 
  BonusVal.value = newBonus; 
  txtNumber.value = newNumber;
}

function dump(e, character, stat) //Allocates "5" with CTRL+right-click//
{
  if (e.ctrlKey == 1 && e.which == 1) {
    debugger;
    console.log('met');
    var txtNumber = document.getElementById(character[stat]);
    var newNumber = parseInt(txtNumber.value) + 4;
    if(newNumber > character.Limits[stat].max) return;
    var BonusVal = document.getElementById(character.Bonus);
    if(BonusVal.value <= 0) return;
    var newBonus = parseInt(BonusVal.value) - 4;
    if( newBonus < 0 ){ //try if is negative

      newBonus = 0; //and fix it
      newNumber = +(BonusVal.value);

    } 
    BonusVal.value = newBonus; 
    txtNumber.value = newNumber;
  }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top