2011-03-20 51 views
0

我有一個益智遊戲,其中件圍繞屏幕拖動但不能重疊。如果它們嘗試重疊,則它們的位置將更改回它們不重疊的位置,並使用invalidate()重新繪製UI。除非關閉應用程序並重新啓動應用程序或更改方向時,除非該活動被銷燬並重新構建,否則該方法運行良好。活動重新啓動後功能發生變化

什麼似乎發生的是,我用於碰撞(x,y,右,底等)的位置變量不會重置爲它們在構造函數中如何初始化。碎片與不可見物體相撞,撞擊到看起來隨機的位置,或者移動不規則。

解決它的唯一方法是手動殺死應用程序(如使用任務殺手)或重新安裝它。然後它會正常工作,直到遊戲活動再次創建。我究竟做錯了什麼?有任何想法嗎?這裏的一塊是如何在OnCreate()添加我GameView類中:

Pieces redtiki = new Pieces(this,0,0,R.drawable.tikired); 
... 
board.addView(redtiki); 

這是我的小品類的一部分:

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; 
    } 

回答

1

在你的Activity類,實現方法的onPause( ),OnResume()。

使用上述方法中的日誌語句來查看在關閉/打開應用程序期間位置/座標是否發生更改。

當調用「OnPause()」時,可以保存各個組件的狀態。當調用「OnResume()」(即應用程序到達前臺)時,讀取已保存的狀態並使用您最近讀取的座標繪製視圖。

請不要在創建活動時僅調用一次「OnCreate()」。切換方向不會調用「OnCreate()」

您將找到解釋Android API文檔的流程圖。 http://developer.android.com/reference/android/app/Activity.html

+0

感謝您的回答。你對onCreate()只調用一次是正確的。我誤解了方向變化時會發生什麼。它會銷燬活動,但它不會再次調用onCreate()。我正在自學編程,雖然我已經學會了一些關於Android的知識,但我不確定如何使用日誌語句。你知道有什麼好的資源可以讓我開始嗎?謝謝你的幫助。 – Amplify91 2011-03-20 06:20:24

+1

@ Amplify91日誌語句用於向控制檯輸出/打印信息。 [http://developer.android.com/reference/android/util/Log.html] [http://stackoverflow.com/questions/2018263/android-logging]第二個鏈接將幫助您定製/增強日誌功能。我會建議開始使用第一個鏈接,並且在您理解/使用該概念後,您可以前進到第二個鏈接。 – 2011-03-20 11:41:39

+0

再次感謝您的幫助!我已經解決了我的問題,並且我也學會了如何使用日誌消息。感謝您的幫助和有用的鏈接。 – Amplify91 2011-04-05 00:12:31

0

我剛剛意識到出了什麼問題。每次創建新的Pieces對象時,它都會添加到我的ArrayList中,即aPieces。所以每當方向發生變化,或者我加載了一個新的級別,它會加載我所有的新件並顯示它們,但是我的舊件仍然在ArrayList中,因此仍然在檢查衝突。爲了解決這個問題,在我加載每個新等級(以及組成等級的所有部分)之前,我先用Pieces.aPieces.clear();清除列表。現在,它完美運作。