Ok, we need to work in a couple of points. First, you need to properly format your code in the forum. To do so, edit your post, highlight your code and then hit ctrl+shift+c so it shows like this
void setup(){}
void draw(){
}
Now for the programing part. First point. When using classes, the name of classes start with upper case. In your MCVE, enemy and bullet class should be Enemy and Bullet. Variables should start with lower letters. It is a convention that you should get used to. Related to arrayList, it is better if you defined their type when they are created. For instance:
ArrayList<Enemy> e = new ArrayList();
This change will make your provided MCVE work. Now, with a working version, we can see what the problem is. I am guessing your main problem is that when you are removing your elements in your arrayList, you are not removing it backwards, as described in the documentation here:
// If you are modifying an ArrayList during the loop,
// then you cannot use the enhanced loop syntax.
// In addition, when deleting in order to hit all elements,
// you should loop through it backwards, as shown here:
for (int i = particles.size() - 1; i >= 0; i--) {
Particle part = particles.get(i);
if (part.finished()) {
particles.remove(i);
}
}
Now, there is another few problems with your design. I won’t solve them all but it is for you to figure what to do (part of this thing called learning…)
Consider the following code:
for (int p = 0; p<e.size(); p++) {
for (int i = 0; i<b.size(); i++) {
bullet a = b.get(i);
enemy o = e.get(p);
if (a.update()) {
b.remove(i);
}
if (o.col()) {
b.remove(i);
e.remove(i);
}
}
Notice you are removing the current bullet object more than once. First of all, you are calling b.remove(i) twice in your code. Although these both might not get called at the same time, it will lead to painful headaches in the future. More subtle and mandates for change, you are removing a bullet in a nested loop. You need to make sure remove gets called only once. Another subtle issue with your code is the index you are using to remove the enemy. Notice the for loop counter for the enemy array is p and not i (Check this: for (int p = 0; p<e.size(); p++) {
) then your line should read something like: e.remove(p);
I would rewrite your code more like this:
for (int p = e.size()-1; p>=0; p--) {
enemy o = e.get(p);
if (o.col()) {
e.remove(p);
}
}
for (int i = b.size()-1; i>=0; i--) {
bullet a = b.get(i);
a.update();
}
As I said, this is not the final solution but for you to consider in your design. For this code, you need to figure out when to remove the bullet in the case it hits the enemy. Don’t be shy to scratch my version and come up with your own better version that handles when the bullet leaves the sketch area or when a bullet hits and destroys an enemy. I hope this helps,
Kf