2017-10-20 29 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循环之后)。 –