Javascript - 无法弄清楚如何为基于回合的操作实现while条件

时间:2015-10-26 19:27:01

标签: javascript for-loop while-loop counter

首先想要先说出你真棒,并提前感谢你。现在我的问题。我正在使用HTML5,Javascript和一些Jquery制作一个基于回合制的RPG游戏。除了我试图模仿buff / debuff之外,我几乎所有的东西都很好,比如伤害,轮流等等。想想一个基于转弯的RPG中的视频游戏角色,比如说“最终幻想”或者每次都会中毒并受到伤害的东西。或者是一个饮用药水并且造成5点伤害3个回合的角色。我已经完成了我认为的广泛研究,并试图搞乱下面列出的不同技术:

1)setInterval(为什么它对我不起作用):它使用毫秒但我似乎无法将其实现为基于回合的动作。

2)setTimeout(为什么它对我不起作用):就像setInterval一样,它取决于传递的毫秒数,而不是像点击某些东西那样的计数器。

3)while和For循环(为什么它对我不起作用):我首先创建一个变量来计算我的攻击函数被点击的次数,将计数增加1然后我应用debuff 。比如说这个例子,邪恶角色的健康状态-5为3回合然后在循环执行后重置。所以类似的东西(通过专注于手头的主题的方式来淡化)。我似乎也遇到的普遍问题是我的循环似乎无限循环或当我使用返回false时;或打破;当代码由于某种原因到达该循环时没有任何反应。请帮忙!

//This is a counter for detecting times attack function executed
var attackCounter = 0;
var dragonHealth = 30; 
// Returns a number between 1-10
var bleedChance = Math.floor((Math.random() * 10) + 1); 

// stored into a variable to call later in different function
var attack1 = function() {
$("#kAttack1").click(function() {
// Apply regular damage of -5 to dragons health 
dragonHealth -= 5; 
// Increment turn counter by 1
attackCounter++;
// If I get an 8 or above (%20 chance) I apply the bleed buff of -5 
// health for 3 turns
if (bleedChance >= 8) {
attackCounter = 0;
// THIS IS WHERE I GET STUCK (What do I need to execute so that (1) I 
// apply -5 health to the dragon for 3 turns (2) After I reset the 
// attack counter to 0 how can I make sure that if bleedChance is called
// during the current 3 turns the dragon is taking -5 bleed damage that 
// bleed damage isn't called again thus stacking the effect undesirably
// And lastly (3) when the effect is over allow bleedChance to be able
// to be activated again if I get over. Again my entire game is turn 
//based not in real time. 
}

});
};

2 个答案:

答案 0 :(得分:0)

我将如何做到这一点。

//declare at the top
var bleedEffects = []

...

if (bleedChance >= 8) {
    attackCounter = 0;
    //add to the bleeds
    bleedEffects.push({turnsLeft:3});
}
//loop through the bleeds starting from the end
for(var i = bleedEffects.length - 1; i >=0; i--) {
    //bleed the dragon
    dragonHealth -= 5;
    //decrement the turns
    bleedEffects[i].turnsLeft--;
    //check if we have no turns left
    if(bleedEffects[i].turnsLeft == 0)
        //remove from the array
        bleedEffects.splice(i, 1);
}

答案 1 :(得分:0)

完美!再次感谢FelesMortis。 对于那些可能在将来偶然发现这个线程的人来说,下面是我用来对敌人施加-5点生命值的debuff 3个回合的脚本,当这3个回合被应用时你不能再叠加相同的效果了。

var bleedEffects = []
var attackCounter = 4;
var dragonHealth = 100;

$("#kAttack1").click(function() {

attackCounter ++;
console.log("Attack Counter Is Now :" + attackCounter);
var bleedChance = Math.floor((Math.random() * 10) + 1);
console.log("Bleed Chance Is:" + bleedChance *  10 + "%");

if ((bleedChance >= 8) && (attackCounter >= 4)) {
    attackCounter = 0;
    console.log("Attack Counter Reset To :" + attackCounter);
    //add to the bleeds
    bleedEffects.push({turnsLeft:3});
}
//loop through the bleeds starting from the end
for(var i = bleedEffects.length - 1; i >=0; i--) {
    //bleed the dragon
    dragonHealth -= 5;
    console.log("Dragon's Health Is Now :" + dragonHealth);
    console.log("Dragons bleeds for " + (-attackCounter + 2) + "more turns");
    //decrement the turns
    bleedEffects[i].turnsLeft--;
    //check if we have no turns left
    if(bleedEffects[i].turnsLeft == 0)
        //remove from the array
        bleedEffects.splice(i, 1);
}
});