2014-05-23 32 views
0

我想在GridWorld中控制Bug。我已經嘗試了兩種方法來做到這一點,這兩種方法都沒有實際移動或轉換錯誤。他們都編譯但沒有任何反應。Gridworld中的事件監聽器/掃描器

這裏是一個將被控制在Bug

package info.gridworld.actor; 

import info.gridworld.grid.*; 
import info.gridworld.grid.Location; 

import java.awt.Color; 

public class MazeBug extends Bug { 

public MazeBug() { 
    super(Color.blue); 
} 
public void forward(){ 

    Grid<Actor> gr = getGrid(); 
    if (gr == null) 
     return; 
    Location loc = getLocation(); 
    Location next = loc.getAdjacentLocation(getDirection()); 
    if (gr.isValid(next)) 
     moveTo(next); 
    else 
     removeSelfFromGrid(); 
} 

public void turnRight(){ 
    setDirection(getDirection() + Location.RIGHT); 
} 

public void turnLeft(){ 
    setDirection(getDirection() + Location.LEFT); 
} 
} 

這裏是我嘗試使用掃描儀控制與鍵W,A,和d的bug(不知道我是否正確使用掃描儀的第一種方式)

package info.gridworld.actor; 

import java.util.Scanner; 
import info.gridworld.grid.*; 

public class KeyTests extends Actor 
{ 
    public static ActorWorld world = new ActorWorld(new BoundedGrid<Actor>(20, 20)); 
    public static MazeBug currentBug; 

    public static void main(String[] args) { 
     world.show(); 
     world.add(new Location(1,11),new MazeBug()); 
     while(true){ 
      Scanner k = new Scanner(System.in); 
      String buttonpress = k.nextLine(); 
      if (buttonpress.equals("w")) 
       currentBug.forward(); 
      if (buttonpress.equals("d")) 
       currentBug.turnRight(); 
      if (buttonpress.equals("a")) 
       currentBug.turnLeft(); 
     }  
    } 
} 

這裏是第二個辦法,我試圖控制錯誤

package info.gridworld.actor; 

import info.gridworld.grid.*; 

public class KeyTests extends Actor 
{ 
    public static ActorWorld world = new ActorWorld(new BoundedGrid<Actor>(20, 20)); 
    public static MazeBug currentBug;  

    public static void main(String[] args) { 

     world.add(new Location(1,11),new MazeBug());  

     java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new  java.awt.KeyEventDispatcher() { 
      public boolean dispatchKeyEvent(java.awt.event.KeyEvent event) { 
       String key = javax.swing.KeyStroke.getKeyStrokeForEvent(event).toString(); 
       if (key.equals("w")) 
        currentBug.forward(); 
       if (key.equals("d")) 
        currentBug.turnRight(); 
       if (key.equals("a")) 
        currentBug.turnLeft(); 
       world.show(); 
       return true; 
      } 
     }); 
     world.show(); 
    } 
} 

感謝您的任何幫助先進的

+0

嘗試將控制器代碼移動到'Bugs'行爲方法中。 – Ferret9

回答

0

我幾乎肯定您的問題是,你把你的控制代碼在Runner而不是你的Bug的行爲方法。

當GridWorld步驟它是所有調用每個Actor'sact方法,因此,如果您Actor有一個無人居住的方法,它只是調用父,或者什麼都不做。你的跑步者,因爲它不是'Actor'沒有act方法,並且第一次跑完之後再也沒有看過。

在MazeBug試試這個:

public void act() 
{ 
    java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new  java.awt.KeyEventDispatcher() { 
     public boolean dispatchKeyEvent(java.awt.event.KeyEvent event) { 
      String key = javax.swing.KeyStroke.getKeyStrokeForEvent(event).toString(); 
      if (key.equals("w")) 
       forward(); 
      if (key.equals("d")) 
       turnRight(); 
      if (key.equals("a")) 
       turnLeft(); 
      return true; 
     } 
    }); 
} 

注意:我從來沒有使用事件偵聽所以無法與代碼的幫助。