拖动和释放触摸后启动球 [英] Launching ball after dragging and releasing touch

查看:122
本文介绍了拖动和释放触摸后启动球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拖动并推出弹,像愤怒的小鸟。拖动部分工作正常,但是当我松开触摸(即ACTION_UP),而其他时候,它的StackOverflowError崩溃有时球立即在边界结束。我应该怎么做,以避免错误和做动作流畅?这里是我的code:

I'm trying to drag and launch a projectile, like Angry Birds. The dragging part works fine, but when I release the touch (i.e ACTION_UP), sometimes the ball instantly ends up at the boundaries while other times, it crashes with StackOverflowError. What should I do to avoid the error and make the movement smooth? Here is my code:

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

任何帮助是AP preciated。谢谢你。

Any help is appreciated. Thank you.

推荐答案

嗯,不从你的错误跟踪的具体信息:一个计算器错误常发生失控的递归函数。对你来说,在推出()如果xCurrent = 0,XMAX = 1000000和xVector = 0.001,你会的可能的创建一个计算器。我会从这里开始调试。

Hmm, without specific information from your error trace: a StackOverFlow error often occurs with a runaway recursive function. For you, in launch() if xCurrent = 0, xMax = 1000000 and xVector = .001, you will probably create a stackoverflow. I would start debugging here.

此外,你似乎有相同的范围内(即xVector和yVector作为类的成员变量里面相同的名字声明变量,它们在也宣告启动(浮动xVector,浮yVector))。这可能会迷惑你,或其他任何人,在路上。尝试使用不同的变量名和传统的命名方案。

Also you seem to declare variables with the same name inside the same scope (ie, xVector and yVector as class member variables and they are also declared in launch(float xVector, float yVector)). This may confuse you, or anyone else, down the road. Try to use distinct variable names and conventional naming schemes.

最后,一个有用的运算符是 + = ,它需要:

Lastly, a useful operator is +=, it takes:

xCurrent = xCurrent + xVector;

和它缩短为:

xCurrent + = xVector;

这可以节省一些不必要的打字。祝你好运!

This saves you some unnecessary typing. Good luck!

这篇关于拖动和释放触摸后启动球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆