有没有简单的方法来写这个?

时间:2015-05-30 21:48:01

标签: javascript

我还在处理我的Text RPG,但是我没有任何简单的方法来编写我的升级代码......这是我得到的:

var LEVEL = 1;
var XP = 0;
var ATTACK = 0;
var STRENGTH = 0;
var DEFENSE = 0;
var RANGED = 0;
var MAGIC = 0;
var AGILITY= 0;
var HEALTH = 0;

if(LEVEL = 1 && XP >= 20) {
    LEVEL = 2;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 2 && XP >= 60) {
    LEVEL = 3;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 3 && XP >= 120) {
    LEVEL = 4;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 4 && XP >= 240) {
    LEVEL = 5;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 5 && XP >= 480) {
    LEVEL = 6;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 6 && XP >= 960) {
    LEVEL = 7;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 7 && XP >= 1920) {
    LEVEL = 8;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 8 && XP >= 3840) {
    LEVEL = 9;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 9 && XP >= 7680) {
    LEVEL = 10;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 10 && XP >= 15360) {
    LEVEL = 11;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 11 && XP >= 30720) {
    LEVEL = 12;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 12 && XP >= 61440) {
    LEVEL = 13;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 13 && XP >= 122880) {
    LEVEL = 14;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 14 && XP >= 245760) {
    LEVEL = 15;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 15 && XP >= 491520) {
    LEVEL = 16;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 16 && XP >= 983040) {
    LEVEL = 17;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 17 && XP >= 1966080) {
    LEVEL = 18;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 18 && XP >= 3932160) {
    LEVEL = 19;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 19 && XP >= 7864320) {
    LEVEL = 20;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

}

我不知道我是否可以使用for()或while()循环,但如果有人可以帮助我,那将节省大量的空间和时间! (顺便说一句,我的每个其他级别所需的xp加倍)

3 个答案:

答案 0 :(得分:2)

您在每个if语句中重复相同的6行代码。这是何时使用函数的完美示例。这为我们提供了代码重用和更好的可读性。

请改为:

注意:您应该在if语句中使用三等于进行比较

if(LEVEL === 1 && XP >= 20){
 LEVEL = 2;
 incrementStats();
}

function incrementStats(){
  ATTACK++;
  STRENGTH++;
  DEFENCE++;
  RANGED++;
  MAGIC++;
  AGILITY++;
}

现在,您只需在每个if语句中调用incrementStats(),而不是每次都输入所有内容。

答案 1 :(得分:2)

您可以使用 levels 参数的单个函数来存储 XP 值,以增加级别,例如

var incrementLevels = (function (){
  var levels = {1:20,2:60,3:120,4:240};  // and so on…
  return function() {
    if (XP >= levels[LEVEL]) {
      LEVEL++;
      ATTACK++;
      STRENGTH++;
      DEFENCE++;
      RANGED++;
      MAGIC++;
      AGILITY++;
    }
  }
}());

我同意elclanrs更喜欢使用小写变量。

修改

上面创建了一个名为 incrementLevels 的函数,其调用方式如下:

incrementLevels();

它使用您指定的全局变量。把它们放在一个对象中会更好,比如叫做 powers (或 abilities 或其他什么):

var powers = {xp: 0, level: 1, attack: 0, strength: 0, 
              defence: 0, ranged: 0, magic: 0, agility: 0};

然后,不是将值设置为全局变量,而是设置对象属性的值,并根据需要将其传递给 incrementLevels 函数。它可能是:

var incrementLevels = (function() {
  var levels = {1:20, 2:60, 3:120, 4:240 };  // and so on…
  var powerList = ['level','attack','strength','defence','ranged','magic','agility'];

  return function(powers) {
    if (powers.xp >= levels[powers.level]) {
      powerList.forEach(function (p){powers[p]++});
    }
  }
}());

并称之为:

incrementLevels(powers);

您可以拥有多个与其他对象关联的 powers 对象,例如 powersA powersB

请注意,包含函数的括号()并不是绝对必要的,但是在您到达底部并查看最终{{{I}之前,指示该函数是一个立即调用的函数表达式(IIFE)非常重要。 1}}。另请参阅Why use (function(){})() or !function(){}()?

一些exmaples:

()

答案 2 :(得分:-1)

var LEVEL = 1;
var XP = 0;
var ATTACK = 0;
var STRENGTH = 0;
var DEFENSE = 0;
var RANGED = 0;
var MAGIC = 0;
var AGILITY = 0;
var HEALTH = 0;

function xpUp(xp){
    if (LEVEL !== countLevel(xp, 1)) {
        LEVEL++;
        ATTACK++;
        STRENGTH++;
        DEFENSE++;
        RANGED++;
        MAGIC++;
        AGILITY++;
        HEALTH++;
    }
    console.log(LEVEL);
}

function countLevel(xp, level){
    if (Math.floor(xp / 60) > 0)
        return countLevel(xp / 2, ++level);
    else
        return level;
}