使用Andengine的游戏中出现意外的arrayIndexOutofBound异常

时间:2012-03-01 03:51:36

标签: android andengine game-engine game-physics

我正在开发一个游戏。一辆汽车从底部移动到顶部(在马路对面),许多汽车正在通过这条路。我想通过触摸和摧毁其他车来拯救单车。我可以得到碰撞。但是当我触摸并摧毁其他车辆时,我在摧毁了两三辆车之后得到了一个异常的ArrayOutOfBound。可能是什么问题?提前致谢

Iterator<Sprite> targets;


public void addTarget() {

    Random rand = new Random();
    int x = (int) mCamera.getWidth() + mTargetTextureRegion.getWidth();
    int minY = mTargetTextureRegion.getHeight();
    int maxY = (int) (mCamera.getHeight() - mTargetTextureRegion
            .getHeight());
    int rangeY = maxY - minY;
    int y = rand.nextInt(rangeY) + minY;
    Sprite target = new Sprite(x, y, mTargetTextureRegion.deepCopy()) {
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                float pTouchAreaLocalX, final float pTouchAreaLocalY) {

            System.out.println("Touched");
             removeSprite(this, targets);

            count--;

            return mChildrenIgnoreUpdate;

        }
    };
    mMainScene.registerTouchArea(target);
    mMainScene.setTouchAreaBindingEnabled(true);

    mMainScene.attachChild(target);
    int minDuration = 10;
    int maxDuration = 20;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = rand.nextInt(rangeDuration) + minDuration;
    MoveXModifier mod = new MoveXModifier(actualDuration, target.getX(),
            -target.getWidth());
    target.registerEntityModifier(mod.deepCopy());
    TargetsToBeAdded.add(target);

}

public void removeSprite(final Sprite _sprite, Iterator it) {
    runOnUpdateThread(new Runnable() {
        @Override
        public void run() {
            mMainScene.detachChild(_sprite);
        }
    });
    it.remove();
}

private void createSpriteSpawnTimeHandler() {
    if (count <= 2) {
        TimerHandler spriteTimerHandler;
        float mEffectSpawnDelay = 1f;
        spriteTimerHandler = new TimerHandler(mEffectSpawnDelay, true,
                new ITimerCallback() {
                    @Override
                    public void onTimePassed(TimerHandler pTimerHandler) {

                        count++;
                        addTarget();

                    }
                });

        getEngine().registerUpdateHandler(spriteTimerHandler);
    }
}

IUpdateHandler detect = new IUpdateHandler() {
    @Override
    public void reset() {
    }

    @Override
    public void onUpdate(float pSecondsElapsed) {
        targets = targetLL.iterator();
        Sprite _target;
        while (targets.hasNext()) {
            _target = targets.next();
            if (_target.getX() <= -_target.getWidth()) {
                removeSprite(_target, targets);
            }

            if (_target.collidesWith(player)) {
                System.out.println("Collission occured");
                // break;

            }

        }
        targetLL.addAll(TargetsToBeAdded);
        TargetsToBeAdded.clear();
    }
};   

1 个答案:

答案 0 :(得分:1)

它主要发生在游戏的更新线程中,因为它会分离儿童的电话。我所做的是用try catch包围mupdate处理程序。我没有时间去解决问题:)