2012-03-01 65 views
1

我在andengine上製作遊戲。一輛汽車從下往上(在馬路對面),許多汽車正在通過馬路。我想通過觸摸和摧毀其他車輛來保存單車。我可以得到碰撞。但是當我觸碰並摧毀其他車輛時,在破壞兩三輛車後,我得到一個異常ArrayOutOfBound。可能是什麼問題?在此先感謝意外的arrayIndexOutofBound在使用Andengine的遊戲中發生異常

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

它主要發生在遊戲的更新線程它是由於分離兒童電話。我所做的是用try catch包圍mupdate處理程序。我沒有時間去解決問題:)

相關問題