2013-03-27 90 views
2

我正在爲我的計算機科學類製作遊戲,並試圖移動一個用箭頭鍵擴展Bug的角色對象。我應該把代碼移動到Character類還是World類的箭頭鍵中?代碼應該是什麼樣子?現在我已經在Character類中獲得了這個代碼,它符合要求,但是當我嘗試在網格中運行它時,按下箭頭鍵時不會發生任何事情。如何使用箭頭鍵在GridWorld中移動一個Bug

public class Character extends Bug 
{ 
Random pokemon; 
public Character() 
{ 

} 

public void act(KeyEvent e) 
{ 
     move(e); 
     pokemon = new Random(); 
     if(pokemon.nextInt(10) == 5) 
      System.out.println("It works!!"); 
} 

public void move(KeyEvent e) 
{ 
    Grid<Actor> gr = getGrid(); 
    Location loc = getLocation(); 
    if(gr == null) 
     return; 
    if(e.getKeyCode() == KeyEvent.VK_RIGHT) 
    { 
     if(!(getDirection() == 90)) 
      setDirection(90); 
     else 
     { 

      Location next = loc.getAdjacentLocation(getDirection()); 
      if (gr.isValid(next)) 
       moveTo(next); 
      else 
       removeSelfFromGrid(); 
     } 
    } 
    else if(e.getKeyCode() == KeyEvent.VK_LEFT) 
    { 
     if(!(getDirection() == 270)) 
      setDirection(270); 
     else 
     { 

      Location next = loc.getAdjacentLocation(getDirection()); 
      if (gr.isValid(next)) 
       moveTo(next); 
      else 
       removeSelfFromGrid(); 
     } 
    } 
    else if(e.getKeyCode() == KeyEvent.VK_UP) 
    { 
     if(!(getDirection() == 0)) 
      setDirection(0); 
     else 
     { 

      Location next = loc.getAdjacentLocation(getDirection()); 
      if (gr.isValid(next)) 
       moveTo(next); 
      else 
       removeSelfFromGrid(); 
     } 
    } 
    else if(e.getKeyCode() == KeyEvent.VK_DOWN) 
    { 
     if(!(getDirection() == 180)) 
      setDirection(180); 
     else 
     { 

      Location next = loc.getAdjacentLocation(getDirection()); 
      if (gr.isValid(next)) 
       moveTo(next); 
      else 
       removeSelfFromGrid(); 
     } 
    } 

public class Character extends Bug 
{ 
Random pokemon; 
public Character() 
{ 

} 

public void act(KeyEvent e) 
{ 
     move(e); 
     pokemon = new Random(); 
     if(pokemon.nextInt(10) == 5) 
      System.out.println("It works!!"); 
} 

public void move(KeyEvent e) 
{ 
    Grid<Actor> gr = getGrid(); 
    Location loc = getLocation(); 
    if(gr == null) 
     return; 
    if(e.getKeyCode() == KeyEvent.VK_RIGHT) 
    { 
     if(!(getDirection() == 90)) 
      setDirection(90); 
     else 
     { 

      Location next = loc.getAdjacentLocation(getDirection()); 
      if (gr.isValid(next)) 
       moveTo(next); 
      else 
       removeSelfFromGrid(); 
     } 
    } 
    else if(e.getKeyCode() == KeyEvent.VK_LEFT) 
    { 
     if(!(getDirection() == 270)) 
      setDirection(270); 
     else 
     { 

      Location next = loc.getAdjacentLocation(getDirection()); 
      if (gr.isValid(next)) 
       moveTo(next); 
      else 
       removeSelfFromGrid(); 
     } 
    } 
    else if(e.getKeyCode() == KeyEvent.VK_UP) 
    { 
     if(!(getDirection() == 0)) 
      setDirection(0); 
     else 
     { 

      Location next = loc.getAdjacentLocation(getDirection()); 
      if (gr.isValid(next)) 
       moveTo(next); 
      else 
       removeSelfFromGrid(); 
     } 
    } 
    else if(e.getKeyCode() == KeyEvent.VK_DOWN) 
    { 
     if(!(getDirection() == 180)) 
      setDirection(180); 
     else 
     { 

      Location next = loc.getAdjacentLocation(getDirection()); 
      if (gr.isValid(next)) 
       moveTo(next); 
      else 
       removeSelfFromGrid(); 
     } 
    } 

這段代碼是否正確的是一個KeyEvent,我怎樣才能從World類調用這段代碼? 任何幫助將不勝感激!

回答

1

ActorWorld類中有一個方法boolean keyPressed(String description, Location loc)此方法是爲了在子類中被重寫的唯一目的。 descriptionKeyStroke,格式爲here,loc爲按下按鍵時光標所在的Location。 (雖然在你的情況下沒關係)

所以總之,你應該擴大KeyPressed在一個自定義CharacterWorld extends ActorWorld類。

+0

謝謝我不知道有一個keyPressed方法,這有點幫助。我仍然堅持要在keyPressed方法中放置什麼,以便我可以使用箭頭鍵移動字符。 – 2013-04-24 21:37:48

+0

您可以使用switch語句來確定字符串描述是「UP」,「DOWN」,「LEFT」還是「RIGHT」。在任何這些情況下,該方法應移動該字符,然後返回true。如果它不是這些擊鍵,則返回false。 – 2013-04-25 02:38:34