0

我有一个视频游戏工具,你可以在这里看到。在页面顶部,您会看到一行蓝色文字描述我的问题。

它在大多数情况下都能正常工作,但如果你足够努力,它会让你进入负数,这是我不想要的。我可以添加任何代码片段来防止负数吗?

我知道代码比它需要的更复杂,主要是因为我是 JavaScript 的新手。我对重写整个事情不感兴趣。只询问我是否缺少明显的修复。

下面是“Monk”类的代码:

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript">

var Human =
    {
      Strength: "HumanStrengthVal",
      Stamina: "HumanStaminaVal",
      Agility: "HumanAgilityVal",
      Dexterity: "HumanDexterityVal",
      Wisdom: "HumanWisdomVal",
      Intelligence: "HumanIntelligenceVal",
      Charisma: "HumanCharismaVal",
      Bonus: "HumanRemainingBonusVal",
      Limits: {
        Strength: {
          max: 100,
          min: 80
        },
        Stamina: {
          max: 100,
          min: 80
        },
        Agility: {
          max: 105,
          min: 85
        },
        Dexterity: {
          max: 105,
          min: 85
        },
        Wisdom: {
          max: 95,
          min: 75
        },
        Intelligence: {
          max: 95,
          min: 75
        },
        Charisma: {
          max: 95,
          min: 75
        }
              }
    };

var Iksar = 
     {
      Strength: "IksarStrengthVal",
      Stamina: "IksarStaminaVal",
      Agility: "IksarAgilityVal",
      Dexterity: "IksarDexterityVal",
      Wisdom: "IksarWisdomVal",
      Intelligence: "IksarIntelligenceVal",
      Charisma: "IksarCharismaVal",
      Bonus: "IksarRemainingBonusVal",
      Limits: {
        Strength: {
          max: 95,
          min: 75
        },
        Stamina: {
          max: 95,
          min: 75
        },
        Agility: {
          max: 120,
          min: 100
        },
        Dexterity: {
          max: 115,
          min: 95
        },
        Wisdom: {
          max: 100,
          min: 80
        },
        Intelligence: {
          max: 95,
          min: 75
        },
        Charisma: {
          max: 75,
          min: 55
        }
              }
    };

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

function add(character, stat)
{
  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)
{
  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)
{
  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)
{
  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;
}
}

</script>
</head>

HTML:

<body>
  
  <table 
  cellpadding='0' 
  cellspacing='0'
  oncontextmenu="table(event)">

    <tr style="background-color:#151515">
  
      <td style="width:102px; padding-top:2px; padding-bottom:3px">
      <b>RACE</b></td>
      <td style="width:40px; padding-top:2px; padding-bottom:3px; color:#00ff00"><b>STR</b></td>
      <td style="padding-top:2px; padding-bottom:3px; color:#00ff00"><b>STA</b></td>
      <td style="padding-top:2px; padding-bottom:3px; color:#00ff00"><b>AGL</b></td>
      <td style="padding-top:2px; padding-bottom:3px; color:#00ff00"><b>DEX</b></td>
      <td style="padding-top:2px; padding-bottom:3px"><b>WIS</b></td>
      <td style="padding-top:2px; padding-bottom:3px"><b>INT</b></td>
      <td style="padding-top:2px; padding-bottom:3px"><b>CHA</b></td>
      <td style="padding-top:2px; padding-bottom:3px"><b>BONUS</b></td>
  
</tr>

<tr>
  
      <td style="background-color:#151515">Human</td>
  
      <td>        
        <input 
        id="HumanStrengthVal" 
        type=“text” 
        value="80" 
        style="width:40px; color:#00ff00"
        onClick="add(Human, 'Strength')" 
        onContextMenu="subtract(Human, 'Strength');"
        onMouseDown="dump(event, Human, 'Strength'); reclaim(event, Human, 'Strength')"
        onfocus="this.blur()"
        />
      </td>
  
      <td>          
        <input 
        id="HumanStaminaVal" 
        type=“text” 
        value="80" 
        style="width:40px; color:#00ff00"
        onClick="add(Human, 'Stamina')" 
        onContextMenu="subtract(Human, 'Stamina');"
        onMouseDown="dump(event, Human, 'Stamina'); reclaim(event, Human, 'Stamina')"
        onfocus="this.blur()"
        />     
      </td>
  
      <td>
      <input 
        id="HumanAgilityVal" 
        type=“text” 
        value="85" 
        style="width:40px; color:#00ff00"
        onClick="add(Human, 'Agility')" 
        onContextMenu="subtract(Human, 'Agility');"
        onMouseDown="dump(event, Human, 'Agility'); reclaim(event, Human, 'Agility')"
        onfocus="this.blur()"
        />
      </td>
  
      <td>
      <input 
        id="HumanDexterityVal" 
        type=“text” 
        value="85"
        style="width:40px; color:#00ff00"
        onClick="add(Human, 'Dexterity')" 
        onContextMenu="subtract(Human, 'Dexterity');"
        onMouseDown="dump(event, Human, 'Dexterity'); reclaim(event, Human, 'Dexterity')"
        onfocus="this.blur()"
        />
      </td>
  
      <td>
      <input 
        id="HumanWisdomVal" 
        type=“text” 
        value="75"
        style="width:40px;"
        onClick="add(Human, 'Wisdom')" 
        onContextMenu="subtract(Human, 'Wisdom');"
        onMouseDown="dump(event, Human, 'Wisdom'); reclaim(event, Human, 'Wisdom')"
        onfocus="this.blur()"
        />
      </td>
  
      <td>
      <input 
        id="HumanIntelligenceVal" 
        type=“text” 
        value="75"
        style="width:40px;"
        onClick="add(Human, 'Intelligence')" 
        onContextMenu="subtract(Human, 'Intelligence');"
        onMouseDown="dump(event, Human, 'Intelligence'); reclaim(event, Human, 'Intelligence')"
        onfocus="this.blur()"
        />
      </td>
  
      <td>
      <input 
        id="HumanCharismaVal" 
        type=“text” 
        value="75"
        style="width:40px;"
        onClick="add(Human, 'Charisma')" 
        onContextMenu="subtract(Human, 'Charisma');"
        onMouseDown="dump(event, Human, 'Charisma'); reclaim(event, Human, 'Charisma')"
        onfocus="this.blur()"
        />
      </td>

      <td>          
        <input 
        id="HumanRemainingBonusVal" 
        type=“text” 
        value="20" 
        style="width:63px;"
        onContextMenu="table(event);"
        onfocus="this.blur()"
        />      
      </td>
  
</tr>

<tr>
  
      <td style="background-color:#151515">Iksar</td>
      
      <td>        
        <input 
        id="IksarStrengthVal" 
        type="text" 
        value="75" 
        style="width:40px; color:#00ff00"
        onClick="add(Iksar, 'Strength')" 
        onContextMenu="subtract(Iksar, 'Strength');"
        onMouseDown="dump(event, Iksar, 'Strength'); reclaim(event, Iksar, 'Strength')"
        onfocus="this.blur()"
        />
      </td>
      
      <td>          
        <input 
        id="IksarStaminaVal" 
        type=“text” 
        value="75" 
        style="width:40px; color:#00ff00"
        onClick="add(Iksar, 'Stamina')" 
        onContextMenu="subtract(Iksar, 'Stamina');"
        onMouseDown="dump(event, Iksar, 'Stamina'); reclaim(event, Iksar, 'Stamina')"
        onfocus="this.blur()"
        />     
      </td>
      
      <td>
      <input 
        id="IksarAgilityVal" 
        type=“text” 
        value="100" 
        style="width:40px; color:#00ff00" 
        onClick="add(Iksar, 'Agility')" 
        onContextMenu="subtract(Iksar, 'Agility');"
        onMouseDown="dump(event, Iksar, 'Agility'); reclaim(event, Iksar, 'Agility')"
        onfocus="this.blur()"
        />
      </td>
      
      <td>
      <input 
        id="IksarDexterityVal" 
        type=“text” 
        value="95" 
        style="width:40px; color:#00ff00" 
        onClick="add(Iksar, 'Dexterity')" 
        onContextMenu="subtract(Iksar, 'Dexterity');"
        onMouseDown="dump(event, Iksar, 'Dexterity'); reclaim(event, Iksar, 'Dexterity')"
        onfocus="this.blur()"
        />
      </td>
      
      <td>
      <input 
        id="IksarWisdomVal" 
        type=“text” 
        value="80" 
        style="width:40px;" 
        onClick="add(Iksar, 'Wisdom')" 
        onContextMenu="subtract(Iksar, 'Wisdom');"
        onMouseDown="dump(event, Iksar, 'Wisdom'); reclaim(event, Iksar, 'Wisdom')"
        onfocus="this.blur()"
        />
      </td>
      
      <td>
      <input 
        id="IksarIntelligenceVal" 
        type=“text” 
        value="75" 
        style="width:40px;" 
        onClick="add(Iksar, 'Intelligence')" 
        onContextMenu="subtract(Iksar, 'Intelligence');"
        onMouseDown="dump(event, Iksar, 'Intelligence'); reclaim(event, Iksar, 'Intelligence')"
        onfocus="this.blur()"
        />
      </td>
      
      <td>
      <input 
        id="IksarCharismaVal" 
        type=“text” 
        value="55" 
        style="width:40px;" 
        onClick="add(Iksar, 'Charisma')" 
        onContextMenu="subtract(Iksar, 'Charisma');"
        onMouseDown="dump(event, Iksar, 'Charisma'); reclaim(event, Iksar, 'Charisma')"
        onfocus="this.blur()"
        />
      </td>

      <td>          
        <input 
        id="IksarRemainingBonusVal" 
        type=“text” 
        value="20" 
        style="width:63px;"
        onContextMenu="table(event);"
        onfocus="this.blur()"
        />      
      </td>
      
</tr>

  </table>

</body>
</html>
4

1 回答 1

-1

我能够解决我的问题。

  if(BonusVal.value >= 0) {
    document.getElementById(character.bonus).value = newBonus;
} else {
    BonusVal.value = 0;
}
于 2013-11-12T22:30:37.900 回答