2016-07-23 38 views
-1

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?

Antwort

1

Die Bewegungen links/rechts und oben/unten unabhängig machen. Sie (oder der Rahmen - ich bin nicht damit vertraut) scheint mit x/y-Koordinaten zu arbeiten, die mathematisch unabhängig sind. So machen Sie eine Funktion, die die Auf-/Ab-Bewegung behandelt, und eine Funktion, die die Links/Rechts-Bewegung behandelt, und Sie können ihre "Kombinationen" loswerden. Gibt es einen Grund, warum die Richtungen positive ganze Zahlen sind? Versuchen Sie, linksbündig = -1, rechts = 1 und keine x-Richtungsbewegung 0 zu machen, und machen Sie dasselbe für y. Das sollte es hoffentlich intuitiver machen.

Glücklich Codierung

+0

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

0

ein Make blooper.direction eine bitweise, wie:

blooper.direction=0; 
if(input.isKeyDown(Input.KEY_RIGHT)) blooper.direction|=1; 
if(input.isKeyDown(Input.KEY_LEFT)) blooper.direction|=2; 
if(input.isKeyDown(Input.KEY_UP)) blooper.direction|=4; 
if(input.isKeyDown(Input.KEY_DOWN)) blooper.direction|=8; 

dann in move den Schritt tun, wie:

if ((blooper.direction&1)!=0) x+=1; 
if ((blooper.direction&2)!=0) x-=1; 
if ((blooper.direction&4)!=0) y-=1; 
if ((blooper.direction&8)!=0) y+=1; 
+0

Ich liebe diesen minimalistischen Ansatz. Ich werde sehen, ob ich das umsetzen kann und Ihnen ein Update geben kann. Vielen Dank! – pocket86