池回收错误2个精灵同时与精灵碰撞

时间:2013-09-30 01:52:19

标签: java android andengine

我有一个射击游戏,你在相反方向射击两颗子弹,所以如果敌人接近子弹的起点(敌人在子弹起点区域内),则会发生错误,

这是我的代码:

if((_target.collidesWith(bullet))){

                    resourcesManager.enemyPool.recyclePoolItem(_target);
                    targets.remove();
                    score=score+1;

                }
                if((_target.collidesWith(bullet2))){
                    resourcesManager.enemyPool.recyclePoolItem(_target);
                    targets.remove();
                    score=score+1;
                    break;
                }

错误日志:

09-30 09:47:23.530: E/AndroidRuntime(1167): in writeCrashedAppName, pkgName :com.www.tangent
09-30 09:47:23.550: I/Process(1167): Sending signal. PID: 1167 SIG: 9
09-30 09:47:23.550: E/AndroidRuntime(1167): FATAL EXCEPTION: UpdateThread
09-30 09:47:23.550: E/AndroidRuntime(1167): java.lang.IllegalStateException
09-30 09:47:23.550: E/AndroidRuntime(1167):     at java.util.LinkedList$LinkIterator.remove(LinkedList.java:164)
09-30 09:47:23.550: E/AndroidRuntime(1167):     at com.www.scene.WeaponSelectScene$1.onUpdate(WeaponSelectScene.java:318)
09-30 09:47:23.550: E/AndroidRuntime(1167):     at org.andengine.engine.handler.UpdateHandlerList.onUpdate(UpdateHandlerList.java:47)
09-30 09:47:23.550: E/AndroidRuntime(1167):     at org.andengine.entity.Entity.onManagedUpdate(Entity.java:1395)
09-30 09:47:23.550: E/AndroidRuntime(1167):     at org.andengine.entity.scene.Scene.onManagedUpdate(Scene.java:284)
09-30 09:47:23.550: E/AndroidRuntime(1167):     at org.andengine.entity.Entity.onUpdate(Entity.java:1167)
09-30 09:47:23.550: E/AndroidRuntime(1167):     at org.andengine.entity.scene.Scene.onManagedUpdate(Scene.java:288)
09-30 09:47:23.550: E/AndroidRuntime(1167):     at org.andengine.entity.Entity.onUpdate(Entity.java:1167)
09-30 09:47:23.550: E/AndroidRuntime(1167):     at org.andengine.engine.Engine.onUpdateScene(Engine.java:591)
09-30 09:47:23.550: E/AndroidRuntime(1167):     at org.andengine.engine.Engine.onUpdate(Engine.java:586)
09-30 09:47:23.550: E/AndroidRuntime(1167):     at org.andengine.engine.LimitedFPSEngine.onUpdate(LimitedFPSEngine.java:56)
09-30 09:47:23.550: E/AndroidRuntime(1167):     at org.andengine.engine.Engine.onTickUpdate(Engine.java:548)
09-30 09:47:23.550: E/AndroidRuntime(1167):     at org.andengine.engine.Engine$UpdateThread.run(Engine.java:820)
09-30 09:47:23.570: I/BstWindowOrientationListener(521): getCurrentRotation(0)
09-30 09:47:23.570: I/ActivityManager(521): Process com.www.tangent (pid 1167) has died.
09-30 09:47:23.570: I/WindowManager(521): WIN DEATH: Window{b4846b48 SurfaceView paused=false}
09-30 09:47:23.590: E/InputDispatcher(521): channel 'b472fa40 com.www.tangent/com.www.tangent.GameActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
09-30 09:47:23.590: E/InputDispatcher(521): channel 'b472fa40 com.www.tangent/com.www.tangent.GameActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
09-30 09:47:23.590: I/WindowManager(521): WIN DEATH: Window{b472fa40 com.www.tangent/com.www.tangent.GameActivity paused=false}

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,没有回答你的问题让我发现了我的问题。

实际上,您在此处同时删除了您的recyclePoolItem,因为在一个帧中,您删除了同一对象的两次(当您的_target与两个对象发生碰撞时发生)。要纠正这个问题,只需用"编写代码,否则如果"而不只是"如果"在" if"语句:

if((_target.collidesWith(bullet))){
    resourcesManager.enemyPool.recyclePoolItem(_target);
    targets.remove();
    score=score+1;
} else if((_target.collidesWith(bullet2))){
    resourcesManager.enemyPool.recyclePoolItem(_target);
    targets.remove();
    score=score+1;
}