Javascript -不知道如何实现基于回合的动作的while条件

Javascript - Cannot figure out how to implement a while condition for turn based actions

本文关键字:于回 条件 while 不知道 何实现 实现 Javascript      更新时间:2023-09-26

首先我想说你太棒了,提前谢谢你。现在说说我的问题。我正在使用HTML5、Javascript和一些Jquery制作一款回合制RPG游戏。除了当我尝试模仿buff/debuff时,我几乎擅长所有事情,比如伤害,轮流等。想想回合制RPG游戏中的电子游戏角色,比如《最终幻想》或其他每回合都会受到伤害的角色。或者一个角色喝了药水,在3个回合内造成5+伤害。我已经做了我认为是广泛的研究,并尝试了下面列出的不同技术:

1) setInterval(为什么它不适合我):它使用毫秒,但我似乎不能实现到一个回合为基础的行动。

2) setTimeout(为什么它不为我工作):像setInterval一样,它取决于毫秒通过而不是计数器,如点击时间。

3) While和For循环(为什么它不适合我):我首先制作一个变量来计算我的攻击函数被点击的次数,增量为1,然后我应用debuff。以这个例子为例,邪恶角色的生命值在3个回合中-5,然后在循环执行后重置。所以类似于(为了专注于手头的话题,这被淡化了)我似乎也有一个普遍的问题是我的循环似乎是无限循环或者当我使用return 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. 
}
});
};

我是这样做的。

//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);
}

完美!再次感谢,FelesMortis。对于那些可能在未来偶然发现这个线程的人来说,下面是我用来在3个回合内对敌人施加-5生命值debuff的脚本,当这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);
}
});