Ich bin brandneu in der Spieleentwicklung und habe beschlossen, mit der Arbeit an einem 2D-Top-Down-Scroller-Spiel zu beginnen. Ich verwende die Slick2D-Bibliothek für dieses Spiel.Java 2D Game Key Eingabe (Best Practice?)
Meine Frage ist über Best Practices für mehrere Richtungseingang für Sprite Bewegung (UP + rechts = Diagonal)
Zur Zeit nehmen, habe ich eine ziemlich hässlich suchen if/elseif Kette in der Eingabe von der Tastatur zu lesen, wird dann in der Klasse 'Mob' überprüft, um festzustellen, wie sich das Sprite bewegt. Das aktuelle Setup funktioniert gut, aber meine Frage ist, ob es eine andere, bessere Möglichkeit gibt, mehrere Eingänge für Diagonalen (oder irgendeine Kombination von Schlüsseln) zu nehmen
Hier ist die Update-Methode der Hauptklasse, die liest der Eingang (blooper ist die Instanz von 'Mob'):
public void update(GameContainer container, StateBasedGame arg1, int delta) throws SlickException {
Input input = container.getInput();
if(input.isKeyDown(Input.KEY_RIGHT)) { //RIGHT
if(input.isKeyDown(Input.KEY_UP)){ //RIGHT + UP
blooper.direction = 2;
} else if(input.isKeyDown(Input.KEY_DOWN)){ //RIGHT + DOWN
blooper.direction = 3;
}
else {
blooper.direction = 1;
}
} else if(input.isKeyDown(Input.KEY_LEFT)){ //LEFT
if(input.isKeyDown(Input.KEY_UP)){ //LEFT + UP
blooper.direction = 5;
} else if(input.isKeyDown(Input.KEY_DOWN)){ //LEFT + DOWN
blooper.direction = 6;
} else{
blooper.direction = 4;
}
} else if(input.isKeyDown(Input.KEY_UP)){ //UP
if(input.isKeyDown(Input.KEY_RIGHT)){ //UP + RIGHT
blooper.direction = 8;
} else if(input.isKeyDown(Input.KEY_LEFT)){ //UP + LEFT
blooper.direction = 9;
} else{
blooper.direction = 7;
}
} else if(input.isKeyDown(Input.KEY_DOWN)){ //DOWN
if(input.isKeyDown(Input.KEY_RIGHT)){ //DOWN + RIGHT
blooper.direction = 11;
} else if(input.isKeyDown(Input.KEY_LEFT)){ //DOWN + LEFT
blooper.direction = 12;
} else{
blooper.direction = 10;
}
} else{
blooper.direction = -1;
}
blooper.update(delta);
}
Und hier ist, wie die Eingabe in der Mob Klasse verarbeitet wird:
public class Mob {
private final int RIGHT = 1;
private final int RIGHTUP = 2;
private final int RIGHTDOWN = 3;
private final int LEFT = 4;
private final int LEFTUP = 5;
private final int LEFTDOWN = 6;
private final int UP = 7;
private final int UPRIGHT = 8;
private final int UPLEFT = 9;
private final int DOWN = 10;
private final int DOWNRIGHT = 11;
private final int DOWNLEFT = 12;
private final int IDLE = -1;
int direction = IDLE;
int x, y;
Image sprite;
public Mob() throws SlickException{
x = 20;
y = 20;
sprite = new Image("res/blooper.png");
}
public void update(int delta){
move();
}
public void draw(){
sprite.draw(x, y);
}
public void move(){
switch(direction){
case RIGHT:
x += 1;
break;
case RIGHTUP:
x += 1;
y -= 1;
break;
case RIGHTDOWN:
x += 1;
y += 1;
break;
case LEFT:
x -= 1;
break;
case LEFTUP:
x -= 1;
y -= 1;
break;
case LEFTDOWN:
x -= 1;
y += 1;
break;
case UP:
y -= 1;
break;
case UPRIGHT:
y -= 1;
x += 1;
break;
case UPLEFT:
y -= 1;
x -= 1;
break;
case DOWN:
y += 1;
break;
case DOWNRIGHT:
y += 1;
x += 1;
break;
case DOWNLEFT:
y += 1;
x -= 1;
break;
case IDLE:
//nothing
}
}
}
wie ich schon sagte ... das funktioniert, aber scheint nicht der beste Weg, um darüber zu gehen. Irgendwelche Tipps?
Danke für den Rat - nicht sicher, warum ich war versuchen, sie zu einer einzigen move() -Methode zu kombinieren. Das sollte funktionieren - ich werde die Änderungen ausarbeiten und Ihnen ein Update geben! Danke noch einmal! – pocket86