【发布时间】:2011-08-31 22:04:05
【问题描述】:
我正在使用池来管理游戏中的子弹。唯一的问题是,当从池中获得子弹时,它刚刚被回收,因为它参与了碰撞,尽管它的 Body 的位置在初始化时使用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;
}
}
}
}
【问题讨论】:
标签: android multithreading collision-detection andengine