2012-04-22 55 views
0

我正在嘗試拖動併發射一個像憤怒的小鳥一樣的彈丸。拖動部分工作正常,但是當我釋放觸摸(例如ACTION_UP)時,有時球會立即在邊界處結束,而在其他時間,它會與StackOverflowError一起崩潰。我應該怎麼做才能避免錯誤並使動作順暢?這裏是我的代碼:拖動並釋放觸摸後啓動球

public class BallView extends View{ 

static Log log; 
Bitmap ball; 
float xStart; 
float yStart; 
float xCurrent; 
float yCurrent; 
int xMax; 
int yMax; 
float xVector; 
float yVector; 

public BallView(Context context){ 
    super(context); 
    this.setFocusable(true); 
    ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball); 
    xStart = 125; 
    yStart = 275; 
    xCurrent = xStart; 
    yCurrent = yStart; 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    xMax = MeasureSpec.getSize(widthMeasureSpec); 
    yMax = MeasureSpec.getSize(heightMeasureSpec); 
    setMeasuredDimension(xMax, yMax); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.drawBitmap(ball, xCurrent, yCurrent, null); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    int eventaction = event.getAction(); 
    int X = (int)event.getX(); 
    int Y = (int)event.getY(); 

    switch (eventaction) { 
    case MotionEvent.ACTION_DOWN: 
     break; 
    case MotionEvent.ACTION_MOVE: 
     xCurrent = X-30; 
     yCurrent = Y-30; 
     break; 

    case MotionEvent.ACTION_UP: 
     Log.d("actionup", "done"); 
     xCurrent = X-30; 
     yCurrent = Y-30; 
     xVector = xStart-xCurrent; 
     yVector = yStart-yCurrent; 
     break; 
    } 
    invalidate(); 
    if (eventaction == MotionEvent.ACTION_UP){ 
     launch(xVector, yVector); 
    } 
    return true; 
} 

private void launch(float xVector, float yVector) { 
    xCurrent = xCurrent + xVector; 
    yCurrent = yCurrent + yVector; 
    if (xCurrent < 0 || xCurrent > xMax || yCurrent < 0 || yCurrent >yMax){ 
     return; 
    } 
    invalidate(); 
    launch(xVector, yVector); 
} 
} 

任何幫助表示讚賞。謝謝。

+0

您應該發佈帶​​有例外的LogCat輸出。 – azertiti 2012-04-22 17:08:01

+0

什麼是xMax和yMax?你有沒有在調試器中完成這一步? – derekerdmann 2012-04-22 17:14:31

回答

0

嗯,沒有你的錯誤軌跡的具體信息:一個StackOverFlow錯誤經常發生失控遞歸函數。對於你,在launch()如果xCurrent = 0,xMax = 1000000和xVector = .001,你會大概創建一個計算器。我會在這裏開始調試。

此外,你似乎在同一範圍內聲明具有相同名稱的變量(即xVector和yVector作爲類成員變量,它們也在launch(float xVector, float yVector)中聲明)。這可能會混淆你或其他任何人。嘗試使用不同的變量名稱和傳統的命名方案。

最後,一個有用的操作是+=,它需要:

xCurrent = xCurrent + xVector;

,並縮短了:

xCurrent += xVector;

這樣可以節省一些不必要的打字。祝你好運!

+0

我添加了睡眠時間,以便遞歸的launch()方法不會經常被調用,它可以工作。謝謝。 – 2012-04-22 21:50:37

0

似乎你的launch()方法是一個不好的遞歸調用。看到這個answer

private void launch(float xVector, float yVector) { 
    new Thread(new Runnable() { 

     @Override 
     public void run() { 
      while (!(xCurrent + xVector < 0 || xCurrent + xVector > xMax || 
        yCurrent + yVector < 0 || yCurrent + yVector > yMax)){ 

       xCurrent = xCurrent + xVector; 
       yCurrent = yCurrent + yVector; 
       try{ 
        Thread.sleep(200); 
       }catch(Exception e){ 
       } 
       postInvalidate(); 
      } 
     } 
    }).start; 
} 
+0

巨大的幫助。謝謝。我會牢記這一點。 – 2012-04-22 21:48:59