2015-02-23 49 views
1

我会尽量清楚,但我可能会错过信息。如果你需要更多的信息来回答,随意问。Javascript战斗转弯系统

我的战斗是其每次我点击一个特定的按钮调用的函数

下面是日志:

var clochardInitiative = 9; 
var Initiative = 10; 

var fightClochard = function() 
{ 
    if (clochardInitiative>Initiative) 
    { 
     HPNow-=(clochardDmg-Armor); 
     clochardLifeNow -= (Dmg-clochardArmor); 
     updateStats(); 
    } 

    else if (Initiative>=clochardInitiative) 
    { 
     clochardLifeNow -= (Dmg-clochardArmor); 
     HPNow-=(clochardDmg-Armor) 
     updateStats(); 
    } 
} 

我想这其中有主动攻击的更高价值的战斗机第一。如果他杀死的是对手。然后对手无法攻击。

现在,即使这些代码中的一个刚刚死亡,两者都正在攻击。

谢谢:)

+1

我没有看到任何测试,一名球员还活着。无论如何,我强烈建议通过为名称添加前缀而创建不同的对象,从而在玩家之间没有不同。 'player.initiative'和'clochard.initiative'。 “HPNow”,“clochardLifeNow”等明显代表相同事物的命名应该具有相同的命名。 – 2015-02-23 14:36:35

+0

我没有看到每个if语句中的代码之间有太大的区别。他们似乎也在做同样的事情。改变秩序并不是做你认为正在做的事情。 – dotnetN00b 2015-02-23 14:37:00

回答

1

你的代码有太多错误。基本上,你的代码应该是这样的:

var Fighter = function (life, armor, initiative, dmg) { 
    this.life = life; 
    this.armor = armor; 
    this.initiative = initiative; 
    this.dmg = dmg; 
}; 

Fighter.prototype.fight = function(opponent) { 
    if (opponent.initiative>this.initiative) 
    { 
     this.life-=(opponent.dmg-this.armor); 
     if(this.life<=0) { 
     updateStats(); 
     return; // <- ANSWER TO YOUR QUESTION 
     } 
     opponent.life-=(this.dmg-opponent.armor); 
     updateStats(); 
    } 
    else if (this.initiative>=opponent.initiative) 
    { 
    opponent.life-=(this.dmg-opponent.armor); 
    if(opponent.life<=0) { 
     updateStats(); 
     return; // <- ANSWER TO YOUR QUESTION 
    } 
    this.life-=(opponent.dmg-this.armor); 
    updateStats(); 
    } 
};  

但我强烈建议看看指南和文件上,然后再继续“面向JavaScript对象”,如here

+0

谢谢。我开始清理我的代码,我会按照你的建议:-) – Boby 2015-02-23 15:04:35

-1

首先在这两种方法你减少双方的生活! Efectivily这意味着它会让你的生活受到打击。

var fightClochard = function() 
{ 
    if (clochardInitiative>Initiative) 
    { 
     HPNow-=(clochardDmg-Armor); 
     //clochardLifeNow -= (Dmg-clochardArmor); <-- commented out because the clochard has hit 
     updateStats(); 
    } 

    else if (Initiative>=clochardInitiative) 
    { 
     clochardLifeNow -= (Dmg-clochardArmor); 
     //HPNow-=(clochardDmg-Armor) <-- commented out because player has hit the clochard. 
     updateStats(); 
    } 
}