arrays - java.util.ConcurrentModificationException with iterator (character movement) -
i'm coding game when hold d character moves right , when hold s character moves left. program works should, 1 exception. i'm moving character (and character moves fine), java keeps throwing error "java.util.concurrentmodificationexception". did research , learned cannot adding arraylist "keys" , iterating @ same time. said, how edit code still have character move seamlessly now? way know of make sure character moves @ same steady pace second hold key down, not take quick movement, pause, , continue motion.
package leveleditor; import java.awt.canvas; import java.awt.dimension; import java.awt.graphics; import java.awt.toolkit; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.event.mousemotionlistener; import java.awt.image.bufferstrategy; import java.util.arraylist; import java.util.iterator; public class leveleditor extends canvas implements keylistener, runnable, mouselistener, mousemotionlistener{ private object[][] grid = new object[50][50]; private graphics buffergraphics = null; //the graphics buffer private bufferstrategy bufferstrategy = null; private thread thread; private boolean running; private int selectedblock = 0; private int mousex; private int mousey; private int playerx; private int playery; private arraylist<integer> keys = new arraylist<integer>(); iterator itr; player player1; public leveleditor(dimension size){ //constructor this.setpreferredsize(size); this.addkeylistener(this); this.thread = new thread(this); this.addmouselistener(this); this.addmousemotionlistener(this); running = true; mousex = 0; mousey = 0; playerx = 0; playery = 0; itr = keys.iterator(); } public void paint(graphics g){ if (bufferstrategy == null){ this.createbufferstrategy(2); bufferstrategy = this.getbufferstrategy(); buffergraphics = bufferstrategy.getdrawgraphics(); player1 = new player(playerx, playery); this.thread.start(); } } @override public void run() { //this runs when level editor running while (running){ //program's logic dologic(); draw(); drawbackbuffertoscreen(); thread.currentthread(); try{ thread.sleep(10); } catch(exception e){ e.printstacktrace(); } } } public void dologic(){ } public void draw(){ //clear secondary screen buffergraphics = bufferstrategy.getdrawgraphics(); try{ buffergraphics.clearrect(0, 0, this.getsize().width, this.getsize().height); //this drawn buffer (int x = 0; x < grid.length; x++){ for(int y = 0; y < grid[x].length; y++){ object o = grid[x][y]; if (o instanceof block){ block blocktodraw = (block)o; blocktodraw.draw(buffergraphics); } } } block.getblock(selectedblock, mousex, mousey).draw(buffergraphics); player1.draw(buffergraphics); (integer x : keys){ if(x == 68 && itr.hasnext()){ playerx += 5; player1.updateplayer(playerx, playery); player1.draw(buffergraphics); itr.next(); itr.remove(); } if (x == 65 && itr.hasnext()){ playerx -= 5; player1.updateplayer(playerx, playery); player1.draw(buffergraphics); itr.next(); itr.remove(); } } } catch(exception e){ e.printstacktrace(); } finally{ buffergraphics.dispose(); } } public void drawbackbuffertoscreen(){ bufferstrategy.show(); toolkit.getdefaulttoolkit().sync(); } @override public void mousereleased(mouseevent e) { int mousex = e.getx(); int mousey = e.gety(); mousex = (mousex / 25); mousey = (mousey / 25); if (e.getmodifiers() == 16){ grid[mousex][mousey] = block.getblock(selectedblock, mousex, mousey); } else if (e.getmodifiers() == 4){ grid[mousex][mousey] = block.getblock(100, mousex, mousey); } } @override public void keytyped(keyevent e) { // todo auto-generated method stub } @override public void keyreleased(keyevent e) { keys.clear(); } @override public void keypressed(keyevent e) { keys.add(e.getkeycode()); } public void mousemoved(mouseevent e) { this.mousex = e.getx() / 25; this.mousey = e.gety() / 25; } @override public void mouseclicked(mouseevent e) { // todo auto-generated method stub } @override public void mousepressed(mouseevent e) { // todo auto-generated method stub } @override public void mouseentered(mouseevent e) { // todo auto-generated method stub } @override public void mouseexited(mouseevent e) { // todo auto-generated method stub } @override public void mousedragged(mouseevent e) { int mousex = e.getx(); int mousey = e.gety(); mousex = (mousex / 25); mousey = (mousey / 25); if(e.mouse_dragged == 506 && e.getmodifiers() == 16){ grid[mousex][mousey] = block.getblock(selectedblock, mousex, mousey); } else if (e.mouse_dragged == 506 && e.getmodifiers() == 4){ grid[mousex][mousey] = block.getblock(100, mousex, mousey); } }
}
using iterator
wrong model here: aren't iterating through list, looking see if have element, , processing if have something.
a better fit use queue - particularly, sort of concurrent queue, e.g. java.util.concurrent.blockingqueue
, since modifying , reading in different threads.
you can add key codes using queue.add(e.getkeycode())
, , remove them using queue.poll()
(or queue.peek()
), returns null value if there nothing in queue (akin itr.hasnext()
being false).
Comments
Post a Comment