0

我正在尝试使用扫描仪来编辑我的塔防游戏的级别。但是,它不会将级别(平铺图像)更新为自定义文件的级别(0 是草 1 是石头 -1 什么都不是,等等)。我发现了错误,但我该如何解决它,我需要添加/更改什么来摆脱它?

java.lang.NullPointerException
    at Levels.loadLevels(Levels.java:11)
    at Window.define(Window.java:28)
    at Window.paintComponent(Window.java:44)

第 11for(int y=0;y<Window.room.block.length;y++) { 行: 第 28levels.loadLevels(new File("levels/level1.level")); 行: 第 44 行:define();

这是扫描仪文件:

import java.io.*;
import java.util.*;

public class Levels {
    public void loadLevels(File loadPath) {
        try {
            Scanner loadLevelsScanner = new Scanner(loadPath);

            while(loadLevelsScanner.hasNext()) {

                for(int y=0;y<Window.room.block.length;y++) {
                    for(int x=0;x<Window.room.block[0].length;x++) {
                        Window.room.block[y][x].groundID = loadLevelsScanner.nextInt();
                    }
                }

                for(int y=0;y<Window.room.block.length;y++) {
                    for(int x=0;x<Window.room.block[0].length;x++) {
                        Window.room.block[y][x].airID = loadLevelsScanner.nextInt();
                    }
                }    
            }               
            loadLevelsScanner.close();

        } catch(Exception e) {              
        }
    }
}

这是窗口文件:

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

public class Window extends JPanel implements Runnable {

    public Thread thread = new Thread(this);        
    public static Image[] tileset_ground = new Image[100];
    public static Image[] tileset_air = new Image[100];     
    public static int myWidth, myHeight;        
    public static boolean isFirst = true;       
    public static Room room;
    public static Levels levels;

    public Window() {
        thread.start();
    }

    public void define() {
        room = new Room();
        levels = new Levels();          
        levels.loadLevels(new File("levels/level1.level"));

        for(int i=0;i<tileset_ground.length; i++) {
            tileset_ground[i] = new ImageIcon("resources/tileset_ground.png").getImage();
            tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(), new CropImageFilter(0, 32 * i, 32, 32)));
        }

        for(int i=0;i<tileset_air.length; i++) {
            tileset_air[i] = new ImageIcon("resources/tileset_air.png").getImage();
            tileset_air[i] = createImage(new FilteredImageSource(tileset_air[i].getSource(), new CropImageFilter(0, 32 * i, 32, 32)));
        }    
    }

    public void paintComponent(Graphics g) {
        if(isFirst) {
            define();               
            isFirst = false;
        }           
        g.clearRect(0, 0, getWidth(), getHeight());         
        room.draw(g);
    }

    public void run() { 
        while(true) {               
            if(!isFirst) {
                room.physic();
            }               
            repaint();              
            try {
                Thread.sleep(1);
            } catch(Exception e) {
            }               
        }           
    }       
}

这是房间文件:

import java.awt.*;

public class Room {
    public int worldWidth = 40;
    public int worldHeight = 20;
    public int blockSize = 32;

    public Block[][] block;

    public Room () { }
    public void define () { }    
    public void physic () { }   

    public void draw(Graphics g) {

        block = new Block[worldHeight][worldWidth];         

        for(int y=0;y<block.length;y++) {
            for(int x=0;x<block[0].length;x++) {
                block[y][x] = new Block(x * blockSize, y * blockSize, blockSize, blockSize, Value.groundGrass, Value.airAir);
                block[y][x].draw(g);
            }
        }
    }    
}

这是块文件:

import java.awt.*;

public class Block extends Rectangle {
    public int groundID;
    public int airID;

    public Block(int x, int y, int width, int height, int groundID, int airID) {
        setBounds(x, y, width, height);

        this.groundID = groundID;
        this.airID = airID;
    }

    public void draw(Graphics g) {
        g.drawImage(Window.tileset_ground[groundID], x, y, width, height, null);

        if(airID != Value.airAir) {
            g.drawImage(Window.tileset_air[airID], x, y, width, height, null);
        }           
    }       
}

最后这是扫描仪应该读取的自定义文件:

1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

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

对不起这个愚蠢的问题,我是初学者。

4

1 回答 1

1

一个快速而肮脏的解决方案是,测试 Window.room 不为空,以及 .block:

        Scanner loadLevelsScanner = new Scanner (loadPath);
        if ((Window.room != null) && 
            (Window.room.block != null)) {
            // ... block until catch block 
        }

到目前为止,我编写的一个简单的 Testapp 工作,如果这样做的话。

但是您需要了解什么是“静态”,以及为什么以及如何使用它。一个常见的初学者错误是,插入“静态”关键字只是为了让编译器保持沉默。

调查,以何种顺序初始化您的类及其属性。

在 Block 中,要访问 Window,您必须有一个参考。引用可以传递给 Block 的 ctor:

class Block extends Rectangle {
    public int groundID;
    public int airID;
    Window window; 

    public Block (int x, int y, int width, int height, int groundID, int airID, Window window) {
        setBounds (x, y, width, height);
        this.groundID = groundID;
        this.airID = airID;
        this.window = window;
    }
    public void draw (Graphics g) {
        g.drawImage (window.tileset_ground [groundID], x, y, width, height, null);
        if (airID != Value.airAir) {
            g.drawImage (window.tileset_air [airID], x, y, width, height, null);
        }
    }
}

谁创建块?它是 Room,所以 Room 本身需要了解 Window(只要你不从根本上改变你的设计)。

public Room (Window w) {
    block = new Block [worldHeight] [worldWidth];
    for (int y=0; y <block.length; y++) {
        for (int x=0; x <block [0].length; x++) {
            block [y] [x] = new Block (x * blockSize, y * blockSize, blockSize, blockSize, Value.groundGrass, Value.airAir, w);
        }
    }
}

块数组被创建、初始化,并且块被传递窗口参数。

在绘图中,您不会一遍又一遍地重新创建数组,也不会重新创建块,而只是重新绘制它们:

public void draw (Graphics g) {
    for (int y=0; y <block.length; y++) {
        for (int x=0; x <block [0].length; x++) {
            block [y] [x].draw (g);
        }
    }
}

在 Window 中,您创建 Room,并将 window-reference 传递给它:

public void define () {
    room = new Room (this);
    levels = new Levels ();
于 2012-05-12T15:09:27.307 回答