在我的游戏球中从上到下,从下到上移动,我的要求是当球接触上边缘或下边缘时我想打印像“游戏结束”之类的祝酒消息。我希望移动控制其他一些意图。使用以下代码为球设置动画
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:
a = 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 and y give you your touch coordinates
a = 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:
a = 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();
}
}