所以基本上当我到达调用精灵的点时,我会在运行游戏时关闭力量。我几乎肯定它与精灵类没有正确编写。如果你不介意的话,我有一个logcat和一些代码来看看。
06-05 11:37:17.475:ERROR / AndroidRuntime(6613):致命异常:Thread-10
06-05 11:37:17.475:错误/ AndroidRuntime(6613):java.lang.NullPointerException
06-05 11:37:17.475:ERROR / AndroidRuntime(6613):at com.game.Game.createZombie1(Game.java:599)
06-05 11:37:17.475:ERROR / AndroidRuntime(6613):at com.game.Game.spawnZombie1(Game.java:642)
06-05 11:37:17.475:ERROR / AndroidRuntime(6613):at com.game.Game.gamePlay(Game.java:344)
06-05 11:37:17.475:ERROR / AndroidRuntime(6613):at com.game.Game.run(Game.java:212)
06-05 11:37:17.475:ERROR / AndroidRuntime(6613):at com.game.GameLoopView $ GameLoopThread.run(GameLoopView.java:51)
游戏类代码(被调用的地方,下一个将是实际的生成)
Zombie1 zom1;
private List<Zombie1> normZombie = new ArrayList<Zombie1>();
private void gamePlay(Canvas canvas) {
elapsed = System.currentTimeMillis()-startTime;
canvas.drawRect(0, 0, width, height, clearPaint);
Rect dst = new Rect(0, 0, width, height);
canvas.drawBitmap(bmpBackground1, null, dst, null);
for (Zombie1 zombie1 : normZombie) {
zombie1.onDraw(canvas);
}
zomAttackSound();
spawnZombie1();
}
void spawnZombie1(){
long now = System.currentTimeMillis()-spawnZom1Time;
if (now > SPAWN_ZOM1_TIME ){
normZombie.add(createZombie1(R.drawable.zombie1));
spawnZom1Time = System.currentTimeMillis();
}
}
这是在Zombie1类中。 (我也尝试过下面的游戏)我很确定导致它的东西就在这里......我已经注释掉了很多代码并将其缩小为产生僵尸的东西。
public Zombie1(GameLoopView gameLoopView, Bitmap bmp) {
this.gameLoopView = gameLoopView;
this.bmp = bmp;
this.width = bmp.getWidth() / BMP_COLUMNS;
this.height = bmp.getHeight() / BMP_ROWS;
Random rnd = new Random();
x = rnd.nextInt(gameLoopView.getWidth() - width);
}
private void update() {
if (y > gameLoopView.getHeight() - height - ySpeed-90 || y + ySpeed < 0) {
game.initGameOver();
ySpeed=0;
}
y = y + ySpeed;
currentFrame = ++currentFrame % BMP_COLUMNS;
}
public void onDraw(Canvas canvas) {
update();
int srcX = currentFrame * width;
int srcY = getAnimationRow() * height;
Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
Rect dst = new Rect(x, y, x + width, y + height);
canvas.drawBitmap(bmp, src, dst, null);
}
}
编辑:哦,我忘了把它放进去。这是在spawnzombie1上面的Game类中
private Zombie1 createZombie1(int resouce) {
Bitmap bmp = BitmapFactory.decodeResource(gameLoopView.getResources(), resouce);
return new Zombie1(gameLoopView, bmp);
}