我正在使用池来管理游戏中的子弹。唯一的问题是,当从池中获得子弹时,它刚刚被回收,因为它涉及到碰撞,虽然它的身体位置Body.setTransform()
在初始化时被重置,但子弹的 Sprite 的位置(用于检测碰撞使用Sprite.collidesWith(otherSprite)
)是重置不够快(因为它在物理线程中更新)。这意味着新创建的子弹会在创建的那一刻引起碰撞,从而导致单个子弹引起多次碰撞。
我尝试在初始化时调用Bullet.sprite.setPosition(0,0)
它,但这显然会干扰,因为在该行代码到位的情况下,子弹根本无法显示。我应该怎么做才能防止这个问题?
子弹创建:
bullets[bulletCounter] = bulletPool.obtainPoolItem();
bullets[bulletCounter].getBody().setTransform(shipBody.getTransform().getPosition(),0);
bullets[bulletCounter].getBody().setLinearVelocity(shipBody.getLinearVelocity());
bullets[bulletCounter].activate();
碰撞检测:
for(int i = 0; i < BULLET_MAX; i++){
if(bullets[i] != null && bullets[i].isActive()){
for(int j = 0; j < enemies.size(); j++){
//check for collision!
if(bullets[i].getSprite().collidesWith(enemies.get(j).getSprite())){
//-snip-
break;
}
}
}
}