how to solve ArrayList outOfBoundsExeption?
- by iQue
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.