我从2天前就开始了一个基本的游戏项目,现在我想制作一个需要将我的球移动到指定位置的水平,我有3个球(我使用ImageView
为我的球):
FstBall=(ImageView)findViewById(R.id.FirstBall);
SecBall=(ImageView)findViewById(R.id.SecondBall);
ThdBall=(ImageView)findViewById(R.id.ThirdBall);
我使用这些方法来移动我的球,但它不起作用,任何想法?
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
int FirstBallX = FstBall.getLeft();
int FirstBallY = FstBall.getTop();
int SecondBallX = SecBall.getLeft();
int SecondBallY = SecBall.getTop();
int ThirdBallX = ThdBall.getLeft();
int ThirdBallY = ThdBall.getTop();
int action = event.getAction();
int x = (int)event.getX();
int y = (int)event.getY();
// Dynamic X and Y :
int FstBaseX= 0;
int FstBaseY= 0;
int SecBaseX= 0;
int SecBaseY= 0;
int ThdBaseX= 0;
int ThdBaseY= 0;
int PrimeyX = 0;
int PrimeryY = 0;
switch (action) {
case MotionEvent.ACTION_DOWN:
if (x > FirstBallX & y > FirstBallY){
FstBaseX = x;
FstBaseY= y;
}else if(x > SecondBallX & y > SecondBallY){
SecBaseX = x;
SecBaseY= y;
}else if(x > ThirdBallX & y > ThirdBallY){
ThdBaseX = x;
ThdBaseY= y;
} else {
// Do Nothing
}
break;
case MotionEvent.ACTION_MOVE:
PrimeyX = x;
PrimeryY = y;
break;
case MotionEvent.ACTION_UP:
if (x > FirstBallX & y > FirstBallY){
int FstFnlResX = FstBaseX + PrimeyX;
int FstFnlResY = FstBaseY + PrimeryY;
FstBall.scrollTo(FstFnlResX, FstFnlResY);
}else if(x > SecondBallX & y > SecondBallY){
int SecFnlResX = SecBaseX + PrimeyX;
int SecFnlResY = SecBaseY + PrimeryY;
SecBall.scrollTo(SecFnlResX, SecFnlResY);
}else if(x > ThirdBallX & y > ThirdBallY){
int ThdFnlResX = ThdBaseX + PrimeyX;
int ThdFnlResY = ThdBaseY + PrimeryY;
ThdBall.scrollTo(ThdFnlResX, ThdFnlResY);
}
break;
}
return true;
}
答案 0 :(得分:1)
你不应该尝试使用setX和setY,然后调用View.invalidate并查看它是否刷新?我不认为在这里使用scrollTo是一个好主意。您可以先尝试调用View.invalidate。如果它不起作用,那么尝试使用setX和setY。
虽然对此有所了解,但它可能有所帮助 Where does Android View.scrollTo(x, y) scroll to?
祝你好运。