Why aren't I getting One Bullet One Hit?
In my collision detection code, bullets are deactivated once they have been involved in a collision:
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;
}
}
The only place bullets are deactivated is in this section of code. The only place they are activated is when they are created.
Despite this a bullet will inflict damage multiple times. removeBullet()
triggers an explosion animation, and this plays multiple times. What could be going wrong?
Update
Here's 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]);
开发者_StackOverflow社区 bullets[i] = null;
}
More than one thread may be running? Alternatively it might not be a problem with removing the bullet. But there are multiple bullets at that position and/or enemies?
Ah AndEngine; I'm actually a Mod on the forum :)
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?
精彩评论