arrays - Java-Tile Map Image Flickering-What can i do to stop this? -
arrays - Java-Tile Map Image Flickering-What can i do to stop this? -
i making pacman game , have map in array, assign different values in map draw different images, in order create pacman map... keeps flickering on place
any help identifying problem wonderful
i realize lot of reading, have looked @ hours , not figured out.... new programming think more experienced eyes benefit me
code --------------------------------------------------------------------------------------------- draw map public void drawmap(){ bufferstrategy bs = this.getbufferstrategy(); do{ do{ graphics g = null; g = getgraphics(); try{ (int x = 0; x < graphics.map.width; x++) { (int y = 0; y < graphics.map.height; y++) { tile = graphics.map.level1[y][x]; //system.out.println(y + ", " + x); int tilex = x * 21;//(int) (x * 21.42857142857143); int tiley = y * 26;//(int) (y * 25.80645161290323); if(tile == 0){ g.drawimage(wall,tilex,tiley, 21, 26,null); //system.out.println(x + ", " + y); } if(tile == 1){ //g.drawimage(space,tilex, tiley, (int)21.42857142857143, (int) 25.80645161290323,null); } } } } { g.dispose(); } } while (bs.contentsrestored()); bs.show(); } while (bs.contentslost()); }
render public void render() { bufferstrategy bs = this.getbufferstrategy(); do{ do{ drawn = false; graphics g = null; try{ g = bs.getdrawgraphics(); if(main_menu == false && game == true) { if(level1test) { if(!drawn) { drawmap(); drawn = true; } //g.drawimage(level1,0,0,getwidth(),getheight(),null); } if(level2test == true ){ g.drawimage(level2,0,0,getwidth(),getheight(),null); } g.drawimage(point,playerx,playery,playerheight, playerwidth,null); } else if(main_menu == false && game == false) { g.drawimage(settingsbackground,0,0,getwidth(),getheight(),null); } else { g.drawimage(background,0,0,getwidth(),getheight(),null); } } { g.dispose(); } } while (bs.contentsrestored()); bs.show(); } while (bs.contentslost()); }
run , timer public void run() { long lasttime = system.nanotime(); double nspertick = 1000000000 / 60d; long lasttimer = system.currenttimemillis(); double delta = 0; int frames = 0; int ticks = 0; while (running == true) { long = system.nanotime(); delta += (now - lasttime) / nspertick; lasttime = now; boolean render = false; while (delta >= 1) { ticks++; tick(); delta -= 1; render = true; } seek { thread.sleep(3); //keep frames going high } grab (interruptedexception e) { e.printstacktrace(); } if(render == true){ frames++; render(); } if (system.currenttimemillis() - lasttimer >= 1000) { lasttimer +=1000; //system.out.println("frames: " + frames + " ticks: " + ticks); frames = 0; ticks = 0; } } }
map array public static int width = 28; public static int height = 31; public static int[][] level1 = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}, {0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,}, {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,}, {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,}, {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,}, {0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0,}, {0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,}, {1,1,1,1,1,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,1,1,1,1,1,}, {1,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1,}, {1,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1,}, {1,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1,}, {1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,}, {1,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1,}, {1,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1,}, {1,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1,}, {1,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1,}, {0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,}, {0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,}, {0,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,}, {0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,}, {0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,}, {0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0,}, {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,}, {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,}, {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,} };
you need double buffer, problem java paints canvas background color , set grahpics instructions, so, if need alter image can in "hidden image" , when it's ready print in canvas area replacing image new rendered image. more info here double buffer in java. edit: forgot, easy it, here illustration double buffer example
java arrays 2d-games pacman
Comments
Post a Comment