【问题标题】:Functionality changes after Activity is restartedActivity重启后功能变化
【发布时间】:2011-03-20 03:52:30
【问题描述】:

我有一个益智游戏,其中的碎片在屏幕上拖动但不能重叠。如果它们试图重叠,它们的位置将更改回它们没有重叠的位置,并且 UI 将使用 invalidate() 重绘。这工作得很好,除非活动被销毁和重建,比如关闭应用程序并重新启动它或改变方向时。

似乎发生的情况是我用于碰撞的位置变量(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;
    }

【问题讨论】:

    标签: java android drag-and-drop collision-detection


    【解决方案1】:

    在您的 Activity 类中,实现方法 OnPause()、OnResume()。

    在上述方法中使用日志语句查看在关闭/打开应用时位置/坐标是否发生变化。

    您可以在调用“OnPause()”时保存各个组件的状态。当调用“OnResume()”时(即应用程序进入前台)读取保存的状态并使用您最近读取的坐标绘制视图。

    请注意,“OnCreate()”只会在创建 Activity 时调用一次。切换方向不会调用“OnCreate()”

    您将找到解释 Android API 文档的流程图。 http://developer.android.com/reference/android/app/Activity.html

    【讨论】:

    • 感谢您的回答。你对 onCreate() 只被调用一次是正确的。我误解了方向改变时会发生什么。它确实会破坏活动,但不会再次调用 onCreate()。我正在自学编程,虽然我学到了很多关于 Android 的知识,但我不确定如何使用日志语句。你知道有什么好的资源可以让我开始吗?感谢您的帮助。
    • @Amplify91 日志语句用于向控制台输出/打印信息。 [developer.android.com/reference/android/util/Log.html] [stackoverflow.com/questions/2018263/android-logging] 第二个链接将帮助您自定义/增强日志记录功能。我建议从第一个链接开始,在您理解/使用该概念后,您可以进入第二个链接。
    • 再次感谢您的帮助!我已经解决了我的问题,并且我也学会了如何使用日志消息。感谢您的帮助和有用的链接。
    【解决方案2】:

    我才意识到出了什么问题。每次我创建一个新的 Pieces 对象时,它都会添加到我的 ArrayList,aPieces。所以每次方向改变,或者我加载了一个新的关卡,它都会加载我所有的新片段并显示它们,但我的旧片段仍然在 ArrayList 中,因此仍然会检查碰撞。为了解决这个问题,在我加载每个新关卡(以及构成关卡的所有部分)之前,我使用Pieces.aPieces.clear(); 清除列表。现在,它完美运行。

    【讨论】:

      猜你喜欢
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 2021-11-06
      • 2021-10-29
      • 1970-01-01
      • 2013-01-25
      相关资源
      最近更新 更多