0

I make remake of old retro game "Space Shooter" I want to display bullet and check if there is hit with enemy

I added all of that but still got this error. I am sure its about bullet

Here code:

    trash = new ArrayList<>();

    for (Bullet bullet: bullets){
        if(bullet.y<0){
            trash.add(bullet);}



            bullet.y = bullet.y+ (int)( -70 * screenRatioY);


        for(Enemy enemy: enemies){
            if(Rect.intersects(enemy.getRectangle(), bullet.getRectangle())){
                score++;
                enemyGetShot.start();
                randomShot = random.nextInt(60-30)+30;
                System.out.println("Nowy random shot: "+randomShot);
                bullet.y=-500;
                enemy.y=-500;

            }
        }
    }
    for(Bullet bullet : trash){
        bullets.remove(bullet);}

and in drawing part:

            for (Bullet bullet : bullets) {

                canvas.drawBitmap(bullet.bullet, bullet.x, bullet.y, paint);

            }

If someone wants a bullet class :

    package com.example.space_shooter;
    
    import android.content.res.Resources;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Rect;
    
    import static com.example.space_shooter.GameView.screenRatioX;
    import static com.example.space_shooter.GameView.screenRatioY;
    
    public class Bullet {
        int x,y,width,height;
    
    
        Bitmap bullet;
    
        Bullet(Resources res){
    
            bullet= BitmapFactory.decodeResource(res, R.drawable.bullet4);
            width= bullet.getWidth();
            height= bullet.getHeight();
            width = (int) (width*1.7);
            height = (int) (height*1.7);
    
            width= (int) (width*screenRatioX);
            height= (int) (height* screenRatioY);
    
            bullet= Bitmap.createScaledBitmap(bullet, width,height, true);
    
    
        }
    
        Rect getRectangle(){
            return new Rect(x,y, x+width, y+height);
        }
    }

Full project : https://github.com/polonez-byte-112/SpaceShooter

  • 1
    You can't remove an element like that while iterating over a list. Use iterators instead. See https://stackoverflow.com/a/18448699/4303296 – Beko Nov 17 '20 at 14:01
  • @notescrew Sorry no WS or anything. Chat here then so that notescrew isn't annoyed. Like I said, I get no ConcurrentModificationException or anything. What exactly is the issue? – Beko Nov 17 '20 at 16:15
  • could u try get like 100 points? – kibat85049 Nov 17 '20 at 16:16
  • Yes, it indeed happened. I'll check it out. – Beko Nov 17 '20 at 16:17
  • Unfortunately I can't reliably reproduce the bug. What I think causes it is that when you add a new bullet (while `update` is deleting one), you get an Exception. Here's code that I believe (I can't say for sure since the bug has only occured like twice or so) should solve that issue (`newBullets` list). I hope that helps. Otherwise I wish you good luck: https://pastebin.com/TR9XZWCm – Beko Nov 17 '20 at 16:46
  • It works but i see that its slow program after some time. it may be becouse those trash arrays arent cleaned idk after all thanks .I will upload link to your profile and this question to github :) – kibat85049 Nov 17 '20 at 17:50
  • i cleared trash and add enemyBullets.removeAll(Trash) without loop and its works perfectly. Thank you soo much <333 – kibat85049 Nov 17 '20 at 17:54
  • That's nice to hear (: Yeah it becomes slower after a while, and it seems you've figured out a reason for that. Good job! – Beko Nov 17 '20 at 18:40

1 Answers1

0

As you continue traversing the list after performing remove().

You're reading and writing to the list at the same time, which breaks the contract of the iterator (internally used for for loop).

If you want to remove the element from the list use Iterator rather (sample code).

trash = new ArrayList<>();

    for (final Iterator<Bullet> iterator = trash.iterator(); iterator.hasNext();) {
      final Bullet student = iterator.next();
      iterator.remove();
    }
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
  • Comments are not for extended discussion or debugging sessions; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/224705/discussion-on-answer-by-notescrew-concurrentmodificationexception-at-displaying). – Cody Gray - on strike Nov 18 '20 at 02:31