Is it possible to have 2 if statements executing at the same time in javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is it possible to have 2 if statements executing at the same time in javascript?

I'm writing a game using canvas in html, css and vanilla java-script. I have made 2 characters and added in their controls for movement. The only problem is that when I execute it I have to wait for player 1 to press whatever key for his character to move, and then player 2 is allowed to input how his character moves. Player 1 uses the wad keys to move and player 2 the arrow keys. I can't figure out how to have them press the buttons at the same time and move at the same time. When I try this the character that's input key was pressed first, will not move while the second character will move. Also if a player wants to jump and move to the right, at the same time, it isn't possible. At the moment the program works by redrawing the characters(squares) in a different space depending on which key is pressed. I was wondering if, simultaneously performing the 'if statements' would solve this. If it won't are there any other solutions to this problem?

4th May 2019, 5:17 PM
BossMan
BossMan - avatar
1 Answer
0
i think the issue here is that you haven't introduced multiple event handlers. what i mean by that is, you have hard bound one event. try nesting your events or use jquery.
7th May 2019, 6:05 AM
Krishnatheja Vanka
Krishnatheja Vanka - avatar