Coding Cat Coding Cat - 6 months ago 12
Java Question

Object not showing up anymore, I don't know why

I had the enemy as a array[] but I recently changed it to an arrayList[] but for some reason nothing shows up anymore, any ideas?

ArrayList<Character> enemy = new ArrayList<Character>(20);
Character enty;

String[] eny = {"Eny_.png" , "eny2.png", "eny3.png"};

for (int i = 0; i < enemy.size(); i ++) {
int ranX = 700 + (int)(Math.random() * 2000);
int ranY = 100 + (int)(Math.random() * 360);
int randE = (int)(Math.random() * 2);

enty = new Character(ranX, ranY,0,0,100,25);
enty.setImage(eny[randE]);
enemy.add(i, enty);
}

for (int i = 0; i < enemy.size(); i ++) {
int ranSpeed = -1 + (int)(Math.random() * -2);
System.out.println(ranSpeed);
enemy.get(i).setVelX(ranSpeed);
enemy.get(i).getVelX();
repaint();
}
}

Answer

Because the 20 in

ArrayList<Character> enemy = new ArrayList<Character>(20)

Is the initial capacity, not the initial size. So,

for (int i = 0; i < enemy.size(); i ++) {

is not doing anything because enemy.size() is zero.

Comments