引用对象的对象

时间:2012-12-06 03:11:53

标签: java android

首先,我试图在Android上制作一个俄罗斯方块克隆,为了学习体验,我已经完成了这个,但代码是一个可怕的混乱,并决定最终回去清理它因为我对发生的事情了解得更多。

但现在似乎我的一个构造函数没有被执行......

我在引用应该在构造函数中初始化的内容时获得NPE,首先将其设置为全局变量。它在这段代码上失败了:

blocksWorld.blockoid.parts.size()

我在GameScreen的构造函数中为BlocksWorld设置了对象,该对象确实执行了:

public GameScreen(Game game) {
    super(game);
    this.screen = AndroidGame.GAME_SCREEN;
    blocksWorld = new BlocksWorld();
}

这是BlocksWorld的构造函数,它似乎没有被执行:

public BlocksWorld() {
    // resets the game world
    for (int x = 0; x < WORLD_WIDTH; x++) {
        for (int y = 0; y < WORLD_HEIGHT; y++) {
            grid[x][y] = 0;
        }
    }
    NewBlockoid(blockoid);
}

NewBlockoid:

public void NewBlockoid(Blockoid blockoid) {
    nextPiece = random.nextInt(7) + 1;
    blockoid = new Blockoid(grid, nextPiece);
}

Blockoid的构造函数:

public Blockoid(int[][] grid, int blockoidType) {
    this.grid = grid;
    this.blockoidType = blockoidType;
    switch (blockoidType) {
    case Blockoid.I:
        color = Color.CYAN;
        parts.add(new BlocksParts(5, 0));
        parts.add(new BlocksParts(5, 1));
        parts.add(new BlocksParts(5, 2));
        parts.add(new BlocksParts(5, 3));
        break;
    case Blockoid.J:
        color = Color.BLUE;
        parts.add(new BlocksParts(5, 0));
        parts.add(new BlocksParts(5, 1));
        parts.add(new BlocksParts(5, 2));
        parts.add(new BlocksParts(4, 2));
        break;
    case Blockoid.L:
        color = Color.rgb(255, 165, 0);
        parts.add(new BlocksParts(5, 0));
        parts.add(new BlocksParts(5, 1));
        parts.add(new BlocksParts(5, 2));
        parts.add(new BlocksParts(6, 2));
        break;
    case Blockoid.O:
        color = Color.YELLOW;
        parts.add(new BlocksParts(5, 0));
        parts.add(new BlocksParts(5, 1));
        parts.add(new BlocksParts(6, 0));
        parts.add(new BlocksParts(6, 1));
        break;
    case Blockoid.S:
        color = Color.GREEN;
        parts.add(new BlocksParts(5, 0));
        parts.add(new BlocksParts(5, 1));
        parts.add(new BlocksParts(4, 1));
        parts.add(new BlocksParts(6, 0));
        break;
    case Blockoid.T:
        color = Color.rgb(128, 0, 128);
        parts.add(new BlocksParts(5, 0));
        parts.add(new BlocksParts(4, 0));
        parts.add(new BlocksParts(6, 0));
        parts.add(new BlocksParts(5, 1));
        break;
    case Blockoid.Z:
        color = Color.RED;
        parts.add(new BlocksParts(5, 0));
        parts.add(new BlocksParts(5, 1));
        parts.add(new BlocksParts(4, 0));
        parts.add(new BlocksParts(6, 1));
        break;
    }
}

有人知道什么是错的吗?是否需要更多代码?谢谢你的帮助......

0 个答案:

没有答案