2011-08-23 29 views
0

當我使用onTouch方法時,我使用方法event.getY()爲變量y2提供手指精度。但有一個問題衝擊片雷管當我使用if語句:onTouch問題 - x <y不作出反應

public boolean onTouch(View v, MotionEvent event){ 
     if(y2 <= knifeY){ 
      //y2 = where the finger is, knifeY = where on the height a knife will be on.    
      knifeDown = false; 
      // The knife stops 
      point--; 
      // score - 1; 
      knife = BitmapFactory.decodeResource(getResources(),R.drawable.bloddyknife); 
      // change picture. 
      return true; 
     } 
    } 

但問題是,當你拿着它在屏幕上,你必須把你的手指,如果你還在抱着它,刀只是擦肩而過。 :(

有人幫忙嗎?

+0

您是否分配了'int y2 = event.getY()'?因爲我沒有在任何地方看到它,所以必須在'onTouch()'內完成 –

回答

1

你指定int y2=event.getY()?因爲我沒有看到它的任何地方,它必須onTouch()

內部完成,我不知道這是好辦法

public boolean onTouch(View v, MotionEvent event) 
{ 
    while(event.getAction()!=MotionEvent.ACTION_CANCEL) // or here measure the pressure applied on the screen surface 
    { 
     if(event.getY() <= knifeY) 
     { 
     knifeDOwn=false; // keep the knife down 
     point--; 
     knife = BitmapFactory.decodeResource(getResources(),R.drawable.bloddyknife); // 
     //maybe here you might stop the pointers collecting with using event.setAction(MotionEvent.ACTION_CANCEL) (or ACTION_UP) to exit the while loop, i dont know your idea for it, at this point i'm just guessing, but you still need to change the action of the event at anypoint at this code to exit the while loop. 
     } 
    } 
    return true; 
} 

我建議你使用GestureListener和使用onFling()用於此目的。

這裏更多閱讀對常量MotionEvent類: http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN

相關問題