2

在我的碰撞检测代码中,子弹一旦发生碰撞就会被停用:

for(int j = 0; j < enemies.size(); j++){
    //check for collision
    if(bullets[i].isActive() && bullets[i].getSprite().collidesWith(enemies.get(j).getSprite())){
    
        //remove bullet  
        removeBullet(i); //bullet is deactivated here, .isActive() will return false
        
        if(enemies.get(j).damage(1)){
            // --snip--
        }
        break;
    }
}

项目符号被停用的唯一地方是在这部分代码中。它们被激活的唯一地方是它们被创建的时候。

尽管如此,一颗子弹会造成多次伤害。removeBullet()触发爆炸动画,并播放多次。可能出了什么问题?

更新

这里是removeBullet()

private void removeBullet(int i){
    if(bullets[i] == null) return;
    bullets[i].deactivate();
    makeSmallExplosion(bullets[i].getSprite().getX(),bullets[i].getSprite().getY());
    bulletPool.recyclePoolItem(bullets[i]);
    bullets[i] = null;
}
4

2 回答 2

2

可能有多个线程在运行?或者,移除子弹可能不是问题。但是那个位置有多个子弹和/或敌人?

于 2011-08-31T14:25:30.553 回答
0

啊和引擎;我实际上是论坛上的Mod :)

I've wrote this blog post about object pools in case you need to check the way you've implemented yours: http://c0deattack.wordpress.com/category/programming/andengine/

I wonder if you're recycling the bullet properly?

于 2011-08-31T15:03:47.517 回答