I’m makeing two-player game. player1
can play with the WASD keys, and player2
can play with the arrow keys.
PlayState.hx
package; import flixel.FlxState; class PlayState extends FlxState { //basic global varible var WindowWidth:Int = 640; var WindowHeight:Int = 480; //sprites var player1:Player; var player2:Player; override public function create():Void { player1 = new Player(50, WindowHeight / 2 - 50); add(player1); player2 = new Player(590, WindowHeight / 2 - 50); add(player2); super.create(); } override public function update(elapsed:Float):Void { player1.update(); player2.update(); super.update(elapsed); } }
Player.hx
package; import flixel.FlxSprite; import flixel.FlxG; import flixel.util.FlxColor; class Player extends FlxSprite { public function new(X:Float,Y:Float) { super(X,Y); makeGraphic(20,100,FlxColor.WHITE); } override public function update(elapsed:Float):Void { super.update(elapsed); } }
In this code, I made a player class. This class is used by both player1
and player2
instances.
Currently both players are moved by the arrow keys. But I want to make two players using different keys (arrow keys and WASD keys).
I’m considering two solutions, but I think the both ways are not perfect. It smells of bad code.
Solution 1: Make player1
and player2
classes. They extend the Player
class and override the update()
method.
class Player1 extends Player { override public function update(elapsed:Float):Void { //make player1 move by wasd keys. super.update(elapsed); } } class Player1 extends Player { override public function update(elapsed:Float):Void { //make player2 move by arrow keys. super.update(elapsed); } }
But if I add the code in update()
, there will be duplicated code. I want to make two player is completely identical, only different location and keyboard.
Solution 2: Receive argument in update()
function which key will use, and use it.
override public function update(elapsed:Float, upkey, downkey, rightkey, leftkey):Void { //I will write it sudo code if(upkey.pressed): move up if(downkey.pressed): move down if(leftkey.pressed): move left if(rightkey.pressed): move right super.update(elapsed); }
But it have to additional argument in the update()
function. And I think it makes the code more complex.
To me, both seem like bad code. What can I do?