how to solve ArrayList outOfBoundsExeption?

Posted by iQue on Game Development See other posts from Game Development or by iQue
Published on 2012-09-02T17:39:54Z Indexed on 2012/09/02 21:49 UTC
Read the original article Hit count: 244

Im getting:

    09-02 17:15:39.140: E/AndroidRuntime(533): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
    09-02 17:15:39.140: E/AndroidRuntime(533):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)

when Im killing enemies using this method:

    private void checkCollision() {
    Rect h1 = happy.getBounds();
    for (int i = 0; i < enemies.size(); i++) {
        for (int j = 0; j < bullets.size(); j++) {
            Rect b1 = bullets.get(j).getBounds();
            Rect e1 = enemies.get(i).getBounds();
            if (b1.intersect(e1)) {
                enemies.get(i).damageHP(5);
                bullets.remove(j);
                Log.d("TAG", "HERE: LOLTHEYTOUCHED");
            }
            if (h1.intersect(e1)){
                happy.damageHP(5);
            }
            if(enemies.get(i).getHP() <= 0){
                enemies.remove(i);
            }
            if(happy.getHP() <= 0){
                //end-screen                           !!!!!!!
            }
        }
    }
}

using this ArrayList:

        private ArrayList<Enemy> enemies = new ArrayList<Enemy>();

and adding to array like this:

   public void createEnemies() {
    Bitmap bmp = BitmapFactory.decodeResource(getResources(),
            R.drawable.female);
    if (enemyCounter < 24) {
        enemies.add(new Enemy(bmp, this, controls));
    }
    enemyCounter++;
}

I dont really understand what the problem is, Ive been looking around for a while but cant really find anything that helps me. If you know or if you can link me someplace where they have a solution for a similar problem Ill be a very happy camper!

Thanks for ur time.

© Game Development or respective owner

Related posts about android

Related posts about collision-detection