2012-04-22 54 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