2014-03-29 36 views
0

遊戲是,用戶試圖在洞中做球。Touchevent run false

1. touchCounter出錯了,我希望用戶只有2次嘗試,當我在遊戲中運行該代碼時,用戶有更多的嘗試,什麼是錯誤?

final int MAX_TRY_COUNT = 2; 
int counter = 0; 

@Override 
    public boolean onTouchEvent(MotionEvent event) { 
    float currentX = event.getX(); 
    float currentY = event.getY(); 
    float deltaX, deltaY; 
    float scalingFactor = 1.0f/((box.xMax > box.yMax) ? box.yMax : box.xMax); 



     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 

       counter = 2; 

    if (currentX >= previousX) { 
    deltaX = currentX - previousX; 
    ball.speedX += deltaX * scalingFactor;  
    } else if (previousX >= currentX) { 
    deltaX = previousX - currentX; 
    ball.speedX += deltaX * scalingFactor; 
    } if (currentY >= previousY) { 
    deltaY = currentY - previousY; 
    ball.speedX += deltaY * scalingFactor;  
    } else if (previousY >= currentY) { 
    deltaY = previousY - currentY; 
    ball.speedY += deltaY * scalingFactor; 
    } 




       break;    
     case MotionEvent.ACTION_UP: 

if (counter >= MAX_TRY_COUNT){ 
       // Alert message 
       return; 
      } 
      counter++; 

     } 



    // Save current x, y 
    previousX = currentX; 
    previousY = currentY; 
    return true; // Event handled 
    } 

回答

0

您需要兩個變量來處理它。

final int MAX_TRY_COUNT = 2; 
int counter = 0; 

在ACTION_DOWN事件

If(counter >= MAX_TRY_COUNT){ 
    // Alert message 
    return; 
} 
counter++; 

,並刪除touch_counter = 1從ACTION_UP

+0

編輯我的問題,像你telled,但是從你如果返回(櫃....){}從eclipse變成紅色下劃線作爲錯誤 – dwayne1991

+0

然後改爲返回false; –

+0

我這樣做,但它不起作用,我可以做比2更多的事件...另一個提示? :) – dwayne1991