我有一个益智游戏,在屏幕上拖动棋子但不能重叠。如果他们尝试重叠,他们的位置会改回到不重叠的位置,并使用invalidate()重新绘制UI。除非在关闭应用程序并重新启动它或更改方向时销毁和重建活动,否则这种方法很有效。
似乎发生的事情是我用于碰撞的位置变量(x,y,right,bottom等)没有重置为在构造函数中初始化它们的方式。碎片与不可见的物体碰撞,与看似随机的位置碰撞,或者不规则地移动。
修复它的唯一方法是手动终止应用程序(如使用任务杀手)或重新安装它。然后它将正常工作,直到第二次创建游戏活动。我究竟做错了什么?有任何想法吗?以下是我在GameView类中的onCreate()中添加片段的方式:
Pieces redtiki = new Pieces(this,0,0,R.drawable.tikired);
...
board.addView(redtiki);
这是我的Pieces课程的一部分:
public class Pieces extends View{
private int x;
private int y;
private int width;
private int height;
private int right;
private int bottom;
private int initialX;
private int initialY;
private Bitmap sizedBitmap;
private Bitmap sourceBitmap;
private int boardSize = 6;
public static ArrayList<Pieces> aPieces = new ArrayList<Pieces>();
//private final Paint mPaint = new Paint();
public Pieces(Context context, int x, int y, int img){
super(context);
this.x = x;
this.y = y;
sourceBitmap = BitmapFactory.decodeResource(getResources(), img);
aPieces.add(this);
initialX=x;
initialY=y;
}
private void sizePiece(){
int scaledWidth;
int scaledHeight;
int h = sourceBitmap.getHeight();
int w = sourceBitmap.getWidth();
if (h>w){
scaledWidth = 1;
}else if (w>h){
scaledWidth = w/h;
}else{
scaledWidth = 0;
}
if (h>w){
scaledHeight = h/w;
}else if (w>h){
scaledHeight = 1;
}else{
scaledHeight = 0;
}
int dstWidth = (((((View) getParent()).getWidth())*scaledWidth)/boardSize)-1;//TODO make sure that -1 is necessary for
int dstHeight = (((((View) getParent()).getHeight())*scaledHeight)/boardSize)-1;//fitting all pieces on the board
sizedBitmap = Bitmap.createScaledBitmap(sourceBitmap, dstWidth, dstHeight, true);
width = sizedBitmap.getWidth();
height = sizedBitmap.getHeight();
right = x+width;
bottom = y+height;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
sizePiece();
canvas.drawBitmap(sizedBitmap, x, y, null);
}
@Override
public boolean onTouchEvent(MotionEvent event){
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//check if touch is on piece
if (eventX > x && eventX < (x+width) && eventY > y && eventY < (y+height)){
initialX=x;
initialY=y;
break;
}else{
return false;
}
case MotionEvent.ACTION_MOVE:
//determine if piece should move horizontally or vertically
if(width>height){
for (Pieces piece : aPieces) {
//if object equals itself in array, skip to next object
if(piece==this){
continue;
}
//check if there the possibility for a horizontal collision
if(this.isAllignedHorizontallyWith(piece)){
//check for and handle collisions while moving left
if(this.isRightOf(piece)){
if(eventX>piece.right+(width/2)){
x = (int)(eventX-(width/2)); //move normally
/*for(Pieces piece2 : aPieces){
if(this.isAllignedHorizontallyWith(piece2)){
if(this.isLeftOf(piece2)){
if(eventX<piece2.x-(width/2)){
x = (int)(eventX-(width/2));
continue;
}else{
x = piece2.x-width-1;
}
}
}
}*/
continue;
}else{
x = piece.right+1;
}
}
//check for and handle collisions while moving right
if(this.isLeftOf(piece)){
if(eventX<piece.x-(width/2)){
x = (int)(eventX-(width/2));
continue;
}else{
x = piece.x-width-1;
}
}
break;
}else{
x = (int)(eventX-(width/2));
}
}
}
if(height>width){
for (Pieces piece : aPieces) {
//if object equals itself in array, skip to next object
if(piece==this){
continue;
}
//check if there the possibility for a vertical collision
if(this.isAllignedVerticallyWith(piece)){
//check for and handle collisions while moving up
if(this.isBelow(piece)){
if(eventY>piece.bottom+(height/2)){
y = (int)(eventY-(height/2)); //move normally
continue;
}else{
y = piece.bottom+1;
}
}
//check for and handle collisions while moving down
if(this.isAbove(piece)){
if(eventY<piece.y-(height/2)){
y = (int)(eventY-(height/2));
continue;
}else{
y = piece.y-height-1;
}
}
break;
}else{
y = (int)(eventY-(height/2));
}
}
}
invalidate();
break;
case MotionEvent.ACTION_UP:
// end move
if(this.moves()){
GameView.counter++;
}
initialX=x;
initialY=y;
break;
}
// parse puzzle
invalidate();
return true;
}
答案 0 :(得分:1)
在Activity类中,实现OnPause(),OnResume()。
方法使用上述方法中的日志语句来查看在关闭/打开应用程序期间是否更改了位置/坐标。
调用“OnPause()”时,可以保存各种组件的状态。当调用“OnResume()”(即应用程序到达前台)时,读取已保存的状态并使用您最近读取的坐标绘制视图。
请注意,只有在创建活动时才会调用“OnCreate()”。切换方向不会调用“OnCreate()”
您将找到解释Android API文档的流程图。 http://developer.android.com/reference/android/app/Activity.html
答案 1 :(得分:0)
我刚刚意识到出了什么问题。每次我创建一个新的Pieces对象时,它都会被添加到我的ArrayList aPieces
中。因此,每次方向改变,或者我加载了一个新级别,它将加载我的所有新部件并显示它们,但我的旧部件仍然在ArrayList中,因此仍然会检查是否存在冲突。为了解决这个问题,在我加载每个新级别(以及组成该级别的所有部分)之前,我使用Pieces.aPieces.clear();
清除列表。现在,它完美无缺。