为什么我的数组仍为NULL

时间:2016-01-15 22:58:58

标签: java arrays

我正在尝试创建一种为游戏布置图块的简单方法。

我是java和OOP的新手,由于某种原因,我通过循环为它们分配变量后,我的数组中的变量仍为null。

我做错了什么,为什么我的数组仍然是NULL?感谢。

        stages stage1 = new stages();

    public void stage1init() {
        stage1.stageW = 30;
        stage1.stageH = 30;
        stage1.tileSize = 100; 
        stage1.stageStartX = 2;
        stage1.stageStartY = 24;
        //Layout Stage1
        int W = stage1.stageH;
        int H = stage1.stageW;
        for(int i = 0; i < H; i++) {
            for(int j = 0; j < W; j++) {
                stage1.TilePositionX[i][j] = 100 * j;
                stage1.TilePositionY[i][j] = 100 * j;
            }
        }
    }


    //Draw current stage

    public void drawStage1() {
        int W = stage1.stageH;
        int H = stage1.stageW;
        for(int i = 0; i < H; i++) {
            for(int j = 0; j < W; j++) {
                saveCurrentTransform();
                translate(stage1.TilePositionX[i][j], stage1.TilePositionY[i][j]);
                drawImage(grasstile, 0, 0, 100, 100);
                restoreLastTransform();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

感谢您的回复,我需要指定数组的大小。

public void stage1init() {
        stage1.stageW = 30;
        stage1.stageH = 30;
        stage1.tileSize = 100; 
        stage1.stageStartX = 2;
        stage1.stageStartY = 24;
        stage1.TilePositionX = new double[stage1.stageW][stage1.stageH];
        stage1.TilePositionY = new double[stage1.stageW][stage1.stageH];
        //Layout Stage1
        int W = stage1.stageH;
        int H = stage1.stageW;
        for(int i = 0; i < H; i++) {
            for(int j = 0; j < W; j++) {
                stage1.TilePositionX[i][j] = 100 * j;
                //EDIT: For anyone looking to do something simular, multipy the following by i not j;
                stage1.TilePositionY[i][j] = 100 * i;
            }
        }
    }