Define two players controls


(Leandro Maguna) #1

Hi guys! I’m trying to make a 1 vs 1 game but i’m failing at setting up the controlls. I want to set A,S,W and D for player one, and Arrows for player two.

My first try was to set Input.define(“LEFT”, Keys.A) for P1 and Input.define(“LEFT”, Keys.RIGHT) but i realize that the last one override the previous one. I google it but couldn’t find anything about this.

Has anybody accomplished something like this? Any advice, tip or whatever would be welcome :smiley:


(Martí Angelats i Ribera) #2

You can simply change the string for every input of every player.

Input.define("P1 left", Keys.A);
Input.define("P2 left", Keys.LEFT);

(Leandro Maguna) #3

Hey! I ended up having a parent class Player and two children PlayerOne and PlayerTwo. The only diference between them is the image and the input key defines. It works better for me.


(Martí Angelats i Ribera) #4

You can actually have a string variable that defines what of the two players is working as prefix. SO then you can use something like

if (Input.pressed(prefix+"left"))
{

}

I like this code more but it’s up to you if you duplicate things.