-4
\$\begingroup\$

I am making an shooting game in Phaser. I want to control the gun using the arrow keys i.e.: upArrow, DownArrow and Enter

upArrow : is to increase the angle
downArrow : is to decrease the angle
Enter : To hit the target

I do not know, how to do this thing?

\$\endgroup\$

closed as off-topic by Kromster, congusbongus, Alexandre Vaillancourt, Maximus Minimus, Anko Feb 12 '17 at 14:10

  • This question does not appear to be about game development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Is this a problem capturing the input or implementing the behaviour? \$\endgroup\$ – Theraot Feb 6 '17 at 19:54
  • \$\begingroup\$ Hello I am learning phaser so I need the sample code for this condition \$\endgroup\$ – kumaresh Feb 7 '17 at 5:23
  • \$\begingroup\$ This doesn't seem like a specific game dev question. Instead it reads more like a request for others to write code for you. \$\endgroup\$ – Pikalek Feb 7 '17 at 7:53
2
\$\begingroup\$

I know very little about Phaser, but for my pure JS/canvas projects I handle key input with eventListeners. Here is how I do it:

var keys = {up:false, left:false, down:false, right:false};
//Add one for every key you want
//Change canvas to the active element
document.getElementById("canvas").addEventListener("keydown", function(e){
   switch(e.keyCode){
      case 38: //38 is keyCode for up arrow
         keys.up=true;
         break;
      case 37: //37 is keyCode for left arrow
         keys.left=true;
         break;
      //And so on... (No default needed)
   }
}
document.getElementById("canvas").addEventListener("keyup", function(e){
   switch(e.keyCode){
      case 38:
         keys.up=false;
         break;
      case 37:
         keys.left=false;
         break;
      //And so on... (No default needed here either)
   }
}
function gameLoop (){
   //Now you can check if the key is pressed or not
   if(keys.up){
      player.moveUp(); //Or whatever you use for moving
   }
   if(keys.left){
      player.moveLeft();
   }
}

I have seen similar solutions, but with an array to save the key presses. To get the keyCodes, I would reccommend using keycode.info.

I hope this helps! (Maybe there is a much simpler way to do it with Phaser)

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.