在onPathFinished之后将精灵发送到池时会出现错误

时间:2013-02-07 06:06:44

标签: android andengine

我有一个精灵(说 Easy Enemy ),它随机方向在场景中移动。当它与新的Sprite( myFood )发生碰撞时,我会播放一个新的动画并让新的方向容易受到攻击。当Easy Enemy完成它的新路径时,我想将它们发送到。在 onPathFinshed()中,我收到错误

我同时使用自定义场景& 动画精灵。还要创建一个 EasyEnemyPool 类来回收它们。

错误:

  

02-07 11:52:55.624:E / AndroidRuntime(22486):致命异常:UpdateThread   02-07 11:52:55.624:E / AndroidRuntime(22486):java.lang.IndexOutOfBoundsException:索引0无效,大小为0   02-07 11:52:55.624:E / AndroidRuntime(22486):at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)   02-07 11:52:55.624:E / AndroidRuntime(22486):at java.util.ArrayList.remove(ArrayList.java:399)

代码内部自定义场景 GameLevelOne 类:

    // own override method
@Override
public void onSceneUpdate() {
    registerUpdateHandler(new IUpdateHandler() {
        @Override
        public void onUpdate(float pSecondsElapsed) {
            synchronized (this) {
                try {
                    // ===get EasyEnemy List from it===
                    eIt = getEasyEnemyIterator();
                    while (eIt.hasNext()) {
                        ee = eIt.next();

                        if (ee.collidesWith(myFoodItem)) {
                            // Log.e("boolean value", ee.isCollisionActive
                            // + "");
                            if (ee.isCollisionActive) {

                                // call the sprite to
                                // move new direction
                                ee.giveNextDirection(ee);
                                unregisterTouchArea(ee);

                                decreaseFoodSize();
                                changeLife();

                                Log.e("!!!!Food !!!: ",
                                        "Easy Enemy Touched My Food");

                                eIt.remove();
                                break;
                            }
                        }
                    }
                    scoreText.setText("Score:" + finalScore);
                    life_text.setText("Life:" + hitpoints + " %");
                } catch (Exception e) {
                    Log.e("Gama Play Engine Error", "" + e);
                }
            }

        }
        @Override
        public void reset() {
        }
    });
}

代码在 EasyEnemy 类中。

public synchronized void giveNextDirection(final EasyEnemy e) {
    clearEntityModifiers();
    animate(new long[] { 110, 110, 110 }, 0, 2, true);
    isCollisionActive = false;
    try {
        registerUpdateHandler(new TimerHandler(2.5f, new ITimerCallback() {

            @Override
            public void onTimePassed(TimerHandler pTimerHandler) {
                // un reg. code
                unregisterUpdateHandler(pTimerHandler);
                // add a path Modifiers
                registerEntityModifier(new PathModifier(5, getFinalPath(),
                        new IPathModifierListener() {

                            @Override
                            public void onPathWaypointStarted(
                                    PathModifier pPathModifier,
                                    IEntity pEntity, int pWaypointIndex) {
                                switch (pWaypointIndex) {
                                case 0:
                                    // move the sprite to right angle
                                    setRotation(270 + getAngle(xCurrent,
                                            yCurrent, xFinal, yFinal));
                                    break;
                                }
                            }

                            @Override
                            public void onPathWaypointFinished(
                                    PathModifier pPathModifier,
                                    IEntity pEntity, int pWaypointIndex) {

                            }

                            @Override
                            public void onPathStarted(
                                    PathModifier pPathModifier,
                                    IEntity pEntity) {

                            }

                            @Override
                            public void onPathFinished(
                                    PathModifier pPathModifier,
                                    IEntity pEntity) {
                        // ===== > remove from the list
                                GamePlayEngine.easyEnemyLinkedList.remove(e);
                                unregisterEntityModifier(pPathModifier);
                        //  ==== > send it to pool
                                EasyEnemyPool.getSharedEasyEnemyPool()
                                        .recyclePoolItem(e);
                            }

                        }));
            }

        }));

    } catch (Exception e2) {
        Log.e("Error", ""+e2);
    }
}

尽我所能了解你的问题。希望,有人发挥超级敲击来解决它。

1 个答案:

答案 0 :(得分:0)

好吧,我发现了我所缺少的一点。我们必须使用 runOnUpdateThread 将一个Sprite发送到Path修饰符侦听器中的池。所以,我的onPathFinish方法看起来像

@Override
                public void onPathFinished(
                    final PathModifier pPathModifier,
                        final IEntity pEntity) {
                            // use BaseActivity runnable thread
                        BaseActivity.instance
                                .runOnUpdateThread(new Runnable() {
                                    @Override
                                    public void run() {

                                    unregisterEntityModifier(pPathModifier);
                                    GamePlayEngine.easyEnemyLinkedList.remove(this);
                                    EasyEnemyPool.getSharedEasyEnemyPool().recyclePoolItem(e);
                                    Log.e(" --onPathFinished-- ", "...No problem..");

                                }
                            });
                    }

希望,这可以帮助某人。