0

I have a sprite (say Easy Enemy) it moves on the scene with a random direction. When it collides with new Sprite (myFood), I play a new animation and give easy enemy to a new direction. When Easy Enemy finished it's new path, I want to send them in Pool. Inside onPathFinshed() i got an error.

I use both custom Scene & Animated Sprite. Also create an EasyEnemyPool class to recycle them.

Error:

02-07 11:52:55.624: E/AndroidRuntime(22486): FATAL EXCEPTION: UpdateThread 02-07 11:52:55.624: E/AndroidRuntime(22486): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 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)

Code Inside custom Scene GameLevelOne class:

    // 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() {
        }
    });
}

code Inside a EasyEnemy class.

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);
    }
}

try my best to understand you the problem. Hope, someone play a super knock to solve it.

4

1 回答 1

0

well, I found the point what I am missing. We have to use runOnUpdateThread to send a Sprite to pool inside Path modifier listener. So,my onPathFinish method looks like

@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..");

                                }
                            });
                    }

Hope, This could help somebody.

于 2013-02-07T09:10:51.820 回答