0

Basically, I want to respond to multiple keys being pressed at the same time. I'm building a little game, where two fighters attack each other. When the user presses A on the keyboard, first fighter attacks, when the user presses J on the keyboard, the second fighter attacks. Then, I would like to have a combo attack, where when the user presses Q W E keys at the same time, It will deal extra damage, so how should I approach this problem ? What I have right now is this :

const getDamage = (attacker,defender) => {
  document.addEventListener("keydown", (e)=>{
    if(e.key ==  controls.playerOneAttack){
      console.log("DEFENDER " + (defender.health = defender.health - ( getHitPower(attacker) - getBlockPower(defender))))
      console.log(attacker.health);
    }
    else if(e.key == controls.playerTwoAttack){
      console.log("ATTACKER " + (attacker.health = attacker.health - (getHitPower(defender) - getBlockPower(attacker))))
      console.log(defender.health);
    }
   
  })
};


const getHitPower = (fighter) => {
  const criticalHitChance = Math.floor(Math.random() * 2 + 1);
  const power = fighter.attack * criticalHitChance;
  return power
};

const getBlockPower = (fighter) => {
  const dodgeChance = Math.floor(Math.random() * 2 + 1);
  const power = fighter.defense * dodgeChance;
  return power
};

I got controls coded in a different file and I'm just passing it in the if statement. Not asking to write a function for me, just asking how to build the if statement to respond to simultaneous key presses. Thank you in advance and sorry for the messy code, I'm a beginner :D

ceaiius
  • 103
  • 1
  • 7
  • 3
    Does this answer your question? [How to detect if multiple keys are pressed at once using JavaScript?](https://stackoverflow.com/questions/5203407/how-to-detect-if-multiple-keys-are-pressed-at-once-using-javascript) – Jeff B May 09 '22 at 13:55
  • Seems like it yeah, if I wrap my mind around that huge answer haha , thanks ! – ceaiius May 09 '22 at 13:59
  • Long answers can be scary, but they usually have some good information! – Jeff B May 09 '22 at 14:00
  • Ye I found something, wasn't scary at all turns out :D can I ask you a quick question, wont take long, if(event.ctrlKey && event.key === "z"){ console.log("YO"); } When i write this, the code works, but when I try to change the keys, for example : if(event.key ==="q" && event.key === "z"){ console.log("YO"); } like this, the code doesnt work anymore, so whats the problem? :D – ceaiius May 09 '22 at 14:32
  • It is because you are checking modifier keys along with letter key presses in the first example. In the second you are checking for just button presses. What you can do is that when "q" is pressed, flip a boolean to true. When it is released bring it to false. Do the same for "z". Then after either is pressed, check if the other is true and do something. I can put a code example if you like. – Jeff B May 09 '22 at 14:35
  • I'd really appreciate it if you showed me a code example, I'd understand better – ceaiius May 09 '22 at 14:38
  • If you don't mind, make this a new question and I will submit an answer for you. It will make it easier to search for if other have the same issue. I will look for the question. – Jeff B May 09 '22 at 14:40
  • 1
    Sure thing! will do – ceaiius May 09 '22 at 14:41
  • I have to wait 90 minutes to post another question so I guess I'll have to wait till then haha – ceaiius May 09 '22 at 14:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244611/discussion-between-jeff-b-and-ceaiius). – Jeff B May 09 '22 at 14:46

0 Answers0