2016-07-23 48 views
-1

我是全新的遊戲開發人員,並已選擇開始製作2D自上而下的滾動遊戲。我正在使用Slick2D庫進行這個遊戲。Java 2D遊戲主要輸入(最佳實踐?)

我的問題是採取多方向輸入精靈運動(UP +右=對角線)

目前的最佳做法,我有一個相當難看,如果/ ELSEIF鏈中從鍵盤輸入讀取然後在'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); 

} 

這裏是輸入是如何在暴民類處理:

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 
     } 
    } 
} 

就像我說的......這個工作,但似乎並不是最好的方法。有小費嗎?

回答

1

使左/右和上/下運動獨立。你(或框架 - 我不熟悉它)似乎與x/y座標一起工作,它們在數學上是獨立的。 因此,使一個功能,對待上/下運動,和一個功能,對待左/右運動,你可以擺脫他們的'組合'。方向是正整數有理由嗎?嘗試使左= -1,右= 1,並沒有x方向運動0,然後對y執行相同操作。這應該有希望使它更直觀。

快樂編碼

+0

謝謝你的建議 - 不知道爲什麼我試圖將它們結合到一個move()方法。這應該工作 - 我會解決這些變化並給你一個更新!再次感謝! – pocket86

0

做一個blooper.direction按位,如:

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; 

然後在move做的舉動,如:

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

我喜歡這種更簡約的方法。我會看看我是否可以實施這個並給你一個更新。謝謝! – pocket86