2017-10-20 75 views
2

我試圖讓我的殭屍在我製作的地圖(使用掃描儀和文件讀取器)中四處移動(使用按鍵),但它只是生成並坐在那裏。由於該程序尚未完成,我仍然有更多的代碼需要執行,但在殭屍移動之前我無法做其他任何事情。預先感謝任何幫助!Sprite不會在我的基於.txt的地圖中移動

PS。 EZ是一個多媒體庫,旨在幫助新手程序員更快地構建包含圖形和聲音的Java應用程序。它在夏威夷的馬諾島使用。

zombie sprite sheet

barbwire

brains

40 26 

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 
M          M 
M         B M 
M  B    B    M 
M          M 
M     M     M 
M M    M     M 
M M    WWW     M 
M WWWW      B  M 
M M         M 
M M         M 
M     B     M 
M  B        M 
M       WWWWWW  M 
M       M   M 
M   B   M   M 
M       M   M 
M       M B  M 
M       M   M 
M  WWWWWWW       M 
M   M       M 
M   M  B     M 
M  B       B M 
M          M 
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 

殭屍

public class Zombie { 

    EZImage zombieSheet; 

    int x = 0;    // Position of Sprite 
    int y = 0; 
    int zombieWidth;  // Width of each sprite 
    int zombieHeight;  // Height of each sprite 
    int direction = 0;  // Direction character is walking in 
    int walkSequence = 0; // Walk sequence counter 
    int cycleSteps;   // Number of steps before cycling to next animation step 
    int counter = 0;  // Cycle counter 

    Zombie(String imgFile, int startX, int startY, int width, int height, int steps) { 
     x = startX;     // position of the sprite character on the screen 
     y = startY; 
     zombieWidth = width;  // Width of the sprite character 
     zombieHeight = height;  // Height of the sprite character 
     cycleSteps = steps;   // How many pixel movement steps to move before changing the sprite graphic 
     zombieSheet = EZ.addImage(imgFile, x, y); 
     setImagePosition(); 
    } 

    private void setImagePosition() { 

     // Move the entire sprite sheet 
     zombieSheet.translateTo(x, y); 

     // Show only a portion of the sprite sheet. 
     // Portion is determined by setFocus which takes 4 parameters: 
     // The 1st two numbers is the top left hand corner of the focus region. 
     // The 2nd two numbers is the bottom right hand corner of the focus region. 
     zombieSheet.setFocus(walkSequence * zombieWidth, direction, walkSequence * zombieWidth + zombieWidth, direction + zombieHeight); 
    } 

    public void moveDown(int stepSize) { 
     y = y + stepSize; 

     direction = 0; 

     if ((counter % cycleSteps) == 0) { 
      walkSequence++; 
      if (walkSequence > 3) 
       walkSequence = 0; 
     } 
     counter++; 
     setImagePosition(); 
    } 

    public void moveLeft(int stepSize) { 
     x = x - stepSize; 
     direction = zombieHeight; 

     if ((counter % cycleSteps) == 0) { 
      walkSequence--; 
      if (walkSequence < 0) 
       walkSequence = 3; 
     } 
     counter++; 
     setImagePosition(); 
    } 

    public void moveRight(int stepSize) { 
     x = x + stepSize; 
     direction = zombieHeight * 2; 

     if ((counter % cycleSteps) == 0) { 
      walkSequence++; 
      if (walkSequence > 3) 
       walkSequence = 0; 
     } 
     counter++; 

     setImagePosition(); 
    } 

    public void moveUp(int stepSize) { 
     y = y - stepSize; 
     direction = zombieHeight * 3; 

     if ((counter % cycleSteps) == 0) { 
      walkSequence--; 
      if (walkSequence < 0) 
       walkSequence = 3; 
     } 
     setImagePosition(); 

     counter++; 
    } 

    // Keyboard controls for moving the character. 
    public void go() { 
     if (EZInteraction.isKeyDown('w')) { 
      moveUp(2); 
     } else if (EZInteraction.isKeyDown('a')) { 
      moveLeft(2); 
     } else if (EZInteraction.isKeyDown('s')) { 
      moveDown(2); 
     } else if (EZInteraction.isKeyDown('d')) { 
      moveRight(2); 
     } 
    } 
} 

ZombieMain

import java.awt.Color; 
import java.io.FileReader; 
import java.util.Scanner; 

public class ZombieMain { 

    static EZImage[] walls = new EZImage[500]; 
    static EZImage[] sideWalls = new EZImage[500]; 
    static EZImage[] brains = new EZImage[50]; 
    static int wallsCount = 0; 
    static int sideWallsCount = 0; 
    static int brainsCount = 0; 

    public static void main(String[] args) throws java.io.IOException { 

     //initialize scanner 
     Scanner fScanner = new Scanner(new FileReader("boundaries.txt")); 

     int w = fScanner.nextInt(); 
     int h = fScanner.nextInt(); 
     String inputText = fScanner.nextLine(); 

     //create backdrop 
     EZ.initialize(w * 33, h * 32); 
     EZ.setBackgroundColor(new Color(0, 0, 0)); 
     Zombie me = new Zombie("zombieSheet.png", 650, 450, 48, 58, 10); 

     //set reading parameters and establish results of case readings 
     for (int row = 0; row < 41; row++) { 

      inputText = fScanner.nextLine(); 

      for (int column = 0; column < inputText.length(); column++) { 

       char ch = inputText.charAt(column); 

       switch (ch) { 
        case 'W': 
         walls[wallsCount] = EZ.addImage("barbwire.jpg", column * 32, row * 32); 
         wallsCount++; 
         break; 
        case 'M': 
         sideWalls[wallsCount] = EZ.addImage("barb.jpg", column * 32, row * 32); 
         wallsCount++; 
         break; 
        case 'B': 
         brains[brainsCount] = EZ.addImage("brains.png", column * 32, row * 32); 
         brainsCount++; 
         break; 
        default: 
         // Do nothing 
         break; 
       } 

       //printed count of walls, side walls, and brains 
       System.out.println("W = " + wallsCount); 
       System.out.println("M = " + sideWallsCount); 
       System.out.println("B = " + brainsCount); 
      } 
     } 
     fScanner.close(); 

     while (true) { 
      me.go(); 
      EZ.refreshScreen(); 
     } 
    } 
} 
+0

代碼看起來合理。如果不查看EZ庫的來源以及它如何處理字符輸入,很難再說更多。 –

+0

http://www2.hawaii.edu/~dylank/ics111/是鏈接到它來源的網頁。 –

+0

你能分享圖像/ boundaries.txt文件嗎? –

回答

0

好吧,明白了。

您使用for循環來迭代帶有映射的文本文件。您的for循環假定您的地圖文件恰好有41行。它不會,因此掃描器在到達循環外部時會拋出異常。

通常,假設讀取文件時固定大小是一個不好的解決方案。你應該做的是使用某種方法(通常與這樣的閱讀器一起提供)來檢查你是否還沒有達到目的,並且使用循環(while)。由於您無論如何需要一個行號,您需要爲其保留一個單獨的計數器,並在每個while循環過程中增加它。

在掃描儀的情況下,你要尋找的方法是fScanner.hasNext(),所以你的代碼應該是這樣的:

int row = 0; 
    //set reading parameters and establish results of case readings 
    while (fScanner.hasNext()) { 
     ... 
     row++; 
    } 
+0

另外,作爲一般規則,請注意控制檯並查看是否有任何錯誤被拋出。如果你的應用程序崩潰了,它不會做任何其他事情。對於圖形應用程序,它可能不會清理其所有線程,因此主代碼可能會崩潰,但圖形引擎可能會繼續運行。另外,學習使用調試器是非常值得的。 –

+0

以及我現在可以移動我的精靈,但我想我可能輸入我的while循環和我的row ++錯誤的地方,因爲現在我的地圖無法正確顯示 –

+0

使用'while'代替外部'for'循環,並記住'row ++'必須位於** external **循環的末尾(即在遍歷列的內部for循環之後)。 –