首先,我正在尝试在 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);
}
新Blockoid:
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;
}
}
有人知道出了什么问题吗?是否需要更多代码?谢谢您的帮助...