Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

100 Neutral

About gamedude7

  • Rank

Personal Information

  • Interests
  1. gamedude7

    Strange error in Bullet Collision in Java

    Okay thank you I get what you're saying so shouldn't public void bulletCollision(){ int array1 = bullets.size(); int array2 = bullets2.size(); for(int i = 0; i<array1; i++){ for(int w = 0; w<array2; w++){ if(bullets.get(i).getBounds().intersects(bullets2.get(w).getBounds())){ bullets.remove(i); bullets2.remove(w); array1--; array2--; w=0; i=0; } } } } Fix the problem? Wouldn't it reset the loop accounting for the new change in size? It still gives me the same error
  2. Hello, I've been working on this 2d game where there are 2 player objects which can shoot in any of the 4 cardinal directions and I've been trying to code for bullet collision. Right now my code for it is public void bulletCollision(){ if(bullets.size()>0){ if(bullets2.size()>0){ for(int i = 0; i<bullets.size(); i++){ for(int w = 0; w<bullets2.size(); w++){ if(bullets.get(i).getBounds().intersects(bullets2.get(w).getBounds())){ bullets.remove(i); bullets2.remove(w); } } } } } } -"bullets" is a CopyOnWriteArrayList of bullets shot from player 1. "bullets2" contains bullets from player 2. -getBounds() returns a rectangle of the same dimensions as the bullet. - bulletCollision() is called in my paintComponent(Graphics g) method. It works just fine except in one case. If player 1 shoots 5 bullets and 4 of them collide with 4 of player 2's bullets, when the last bullet is supposed to collide, this error pops up Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 at java.util.concurrent.CopyOnWriteArrayList.get(Unknown Source) at DrawnClass.bulletCollision(DrawnClass.java:91) at DrawnClass.paintComponent(DrawnClass.java:77) at DrawnClass.paint(DrawnClass.java:51) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.BufferStrategyPaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent._paintImmediately(Unknown Source) at javax.swing.JComponent.paintImmediately(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) - If player 2 has either 1 or 2 bullets out, it works fine. If there are 3 or more bullets out from player 2, when player 1's last bullet onscreen collides, it shows that error - It doesn't happen if the roles are reversed and player 2 has only 1 bullet out. I've been at this for about an hour and any help would be much appreciated
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!