android - how to change the intent when ball touch the bottom or top the canvas -
in game ball moves top bottom , bottom top,my requirement when ball touches top edge or bottom edge want print toast message "game over".later want move control other intent.to animate ball using following code
public class animatedview extends imageview{ static int count=0; private context mcontext; int x = 130; int y = 450; private float a,b; private int xvelocity = 25; private int yvelocity = 20; private handler h; private final int frame_rate = 30; bitmapdrawable ball; boolean touching; boolean dm_touched = false; float move=3; int bm_x = 0, bm_y = 0, bm_offsetx, bm_offsety,bm_w,bm_h; boolean paused; public animatedview(context context, attributeset attrs) { super(context, attrs); mcontext = context; h = new handler(); } @override public void builddrawingcache() { // todo auto-generated method stub super.builddrawingcache(); } private runnable r = new runnable() { @override public void run() { //log.e("game","run called"); if(touching = true) invalidate(); } }; @override protected void ondraw(canvas c) { bitmapdrawable ball = (bitmapdrawable) mcontext.getresources().getdrawable(r.drawable.ball); if (x<0 && y <0) { //x = this.getwidth()/2; y = c.getheight()/2; } else { y += yvelocity; if (y >( this.getheight() - ball.getbitmap().getheight()) ||y <0) { yvelocity = yvelocity*-1; } } c.drawbitmap(ball.getbitmap(), x, y, null); //log.e("sarat",""+touching); if(touching){ // log.e("game","iftouch called called"); h.postdelayed(r, frame_rate); bm_w=ball.getbitmap().getwidth(); bm_h=ball.getbitmap().getheight(); } } @override public boolean ontouchevent(motionevent event) { // log.d("game","ontouch called"); int touchtype = event.getaction(); switch(touchtype){ case motionevent.action_move: = event.getx(); b = event.gety(); touching = true; if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { if(bm_h+a>600 ||bm_h+b<0){ toast.maketext(getcontext(),"game over",toast.length_long).show(); } break; case motionevent.action_down: //x , y give touch coordinates = event.getx(); b = event.gety(); touching = true; log.e("s",""+ a); log.e("s",""+ b); log.e("s",""+ x); log.e("s",""+ y); log.e("s",""+ bm_w); log.e("s",""+ bm_h); if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { count++; mainactivity.setcount(count); log.i("score",""+count); } if (dm_touched) { if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { move+=2; y = yvelocity*-1; }} case motionevent.action_up: = event.getx(); b = event.gety(); if(a>x+20&&a<330&&b<=y+320&&b>y){ invalidate();} if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { log.e("game","clicked"); } default: dm_touched = true; } return true; } @override public void destroydrawingcache() { count=0; super.destroydrawingcache(); } }
Comments
Post a Comment