javascript - How to handle near-simultaneous opposing event functions -


var usera = 10; var userb = 10; var users = [usera, userb];  function user(defence, attack){   (var = 0; < users.length; i++) {   if (users[i] == usera) {     if (defence == attack) {       return 0;  } }    //attack undefended , hurts defender   if (attack == "head") {     return users[i] -= 4;   } else if (attack == "leg") {     return users[i] -= 2;   }  else if (users[i] == userb) {   if (defence == attack) {     return 0;  } }    //attack undefended , hurts defender   if (attack == "head") {     return users[i] -= 4;   } else if (attack == "leg") {     return users[i] -= 2;   }    }  }  users[i] += user("head", "leg"); 

as can see, i'm trying write basic javascript 1-to-1 combat game, i'm struggling..my code not fuctional.

how can make "attack" function , "defend" function interconnected ? example; user a: targets user b's head user a: meanwhile, defends head opponent user b: defends head, therefore user a's damage not done user b: meanwhile, attacks user a's legs , 3 points damage user a

this own project in im trying develop coding skills :) hope understood question! if not, can clafiry bit more. thanks

how can make "attack" function , "defend" function interconnected ?

function effect_of_attack(d_part, a_part){   // attack defended, no effect   if (d_part == a_part) {     return 0;   }    //attack undefended , hurts defender   if (a_part == "head") {     return -4;   } else if (a_part == "leg") {     return -2;   } }  //assume d_part holds defense's input ("leg" or "head") //assume a_part holds attacker's input ("leg" or "head")  defenders_health += effect_of_attack(d_part, a_part); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -