android - In my view something unwanted shows up -
i building snake game app.
fur works, except shows.
reason shows 2 snakes: original snake moves , can controlled, , begginging of snake doesn't anything.
before drew @ every cycle in loop background color, cover last canvas, , there no problem.
then decided divide snake background, have made snake canvas's background transparent , clear canvas each time wants draw new canvas (each loop-to move snake).
here code:
the run of thread:
public void run() { long steppersecond=1000/fps; long starttime; long sleeptime; while(isrunning){ canvas c=null; starttime=system.currenttimemillis(); try{ c=this.getholder().lockcanvas(); synchronized (this.getholder()) { this.ondraw(c); } } catch(exception e){ } finally{ if(c!=null){ this.getholder().unlockcanvasandpost(c); } } sleeptime= steppersecond-(system.currenttimemillis()-starttime); if(sleeptime>0) try { thread.sleep(sleeptime); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
the snake's ondraw (it uses snake's class on draw):
public void ondraw(canvas canvas) { // todo auto-generated method stub super.ondraw(canvas); canvas.drawcolor(color.transparent, mode.clear); snake.ondraw(canvas); }
the snake's class ondraw:
public void ondraw(canvas canvas){ switch(dirtransfare(dir)){ case 1://right xloc=xloc+part_size; break; case 2://up yloc=yloc-part_size; break; case -1://left xloc=xloc-part_size; break; case -2://down yloc=yloc+part_size; break; }//swich int x=xloc; int y=yloc; for(brick bp:snakebody){ bp.setr(0+x, 0+y, part_size+x, part_size+y); canvas.drawrect(bp.getr(),bp.getp()); int xtemp=bp.getx(); int ytemp=bp.gety(); bp.setx(x); bp.sety(y); x=xtemp; y=ytemp; }//for each }//ondraw
here mainactivity, summons snakevew , backgorundview (snake game view , background arena)
public class mainactivity extends activity { gameview gv; imageview left; imageview right; imageview up; imageview down; arena a; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); /*requestwindowfeature(window.feature_no_title); v= new gameview1(this); setcontentview(v); */ gv= new gameview(this); a=new arena(this); setcontentview(r.layout.game_view); linearlayout surface = new linearlayout(this); linearlayout background = new linearlayout(this); surface = (linearlayout)findviewbyid(r.id.surface); background = (linearlayout)findviewbyid(r.id.background); surface.addview(gv); background.addview(a); left=(imageview) findviewbyid(r.id.left); right=(imageview) findviewbyid(r.id.right); up=(imageview) findviewbyid(r.id.up); down=(imageview) findviewbyid(r.id.down); left.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub if(gv.snake.getdir()!="left"&&gv.snake.getdir()!="right"){ gv.snake.left(); } } }); right.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub if(gv.snake.getdir()!="right"&&gv.snake.getdir()!="left"){ gv.snake.right(); } } }); up.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub if(gv.snake.getdir()!="up"&&gv.snake.getdir()!="down"){ gv.snake.up(); } } }); down.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub if(gv.snake.getdir()!="down"&&gv.snake.getdir()!="up"){ gv.snake.down(); } } }); }
the problem not in background (the arena class); have alredy took down , confirmed (it worked same away, bug, without using arena class).
here how looks bug:
http://i.tinyuploads.com/q7oztz.jpg
(you can see 2 snakes - long 1 real one) blue background class arena.
any ideas? tnx :d
Comments
Post a Comment