Hi, my project is 95% done, unfortunately the following final problem is the most important one. Please generously help.
My game contains two players, let’s define them as “player 1” and “player 2”. In my coding, I used key.event function to detect what key is pressed on a real keyboard, then I used an enterFrame event function to control based on the key pressed.
The problem is:
(1) When one of the keys designed to control player 1, let’s say “left” key to control player 1 to move left, is pressed, all of the keys designed to control player 2, e.g. “a” key to control player 2 to move left, cannot be detected. I think it’s because the game can’t detect multiple key inputs, so when any one of the keys is pressed, all other keys can’t be used.
(2) I want to make both player 1 and player 2 able to jump, it’s like when I press “left” and “up” on keyboard at the same time, player 1 will jump to the left, and when I press “d” and “w” at the same time, player 2 will jump to the right.
The following is the related code so far:
function player1Control(event) if (event.phase == "up") then direction = "noMove" elseif (event.keyName == "left" and event.phase == "down") then direction = event.keyName elseif (event.keyName == "right" and event.phase == "down") then direction = event.keyName elseif (event.keyName == "up" and event.phase == "down") then direction = event.keyName end end function player2Control(event) if (event.phase == "up") then direction = "noMove" elseif (event.keyName == "a" and event.phase == "down") then direction = event.keyName elseif (event.keyName == "d" and event.phase == "down") then direction = event.keyName elseif (event.keyName == "w" and event.phase == "down") then direction = event.keyName end end function player1Update() if direction == "left" then player1.x = player1.x - moveSpeed \* 2 elseif direction == "right" then player1.x = player1.x + moveSpeed \* 2 elseif direction == "up" then player1.y = player1.y - moveSpeed \* 5 end end function player2Update() if direction == "a" then player2.x = player2.x - moveSpeed \* 2 elseif direction == "d" then player2.x = player2.x + moveSpeed \* 2 elseif direction == "w" then player2.y = player2.y - moveSpeed \* 5 end end Runtime:addEventListener("key", player1Control) Runtime:addEventListener("enterFrame", player1Update) Runtime:addEventListener("key", player2Control) Runtime:addEventListener("enterFrame", player2Update)