简体   繁体   English

Java Android onTouch方向

[英]Java Android onTouch Direction

I am trying to develop a simple game wherein I need to throw a bullet . 我正在尝试开发一个简单的游戏,其中我需要扔一颗子弹。 I would like the ball follows the touch, or swipe direction. 我希望球跟随触摸或滑动方向。

However, I have a small problem that I have not had success in solving. 但是,我有一个小问题,我没有成功解决。

I take coordinates on the ACTION_DOWN of MotionEvent , and the coordinates on the ACTION_UP of MotionEvent , then calculate the straight line equation in order to move the ball according to this straight line. 我拿上的坐标ACTION_DOWNMotionEvent ,并在坐标ACTION_UPMotionEvent ,然后计算以便根据该直线移动球的直线方程。

The problem is that when I swipe in a direction close to "up" or "forward" or "in front of", the ball moves at lightning speed while in the other direction of more skewed (right or left) or diagonal, the ball moves at a normal speed. 问题是,当我在接近“上”,“前进”或“前方”的方向上滑动时,球以闪电般的速度移动,而在另一个方向上更偏斜(向右或向左)或对角线移动以正常速度移动。

Where is the flaw in my calculation? 我的计算中的缺陷在哪里?

Help me please, I am not far from the result but need to you for solve this problem! 请帮助我,我离结果不远,但需要您解决这个问题!

The onTouch method: onTouch方法:

public boolean onTouchEvent(MotionEvent event) {
    switch(event.getAction()) {
        case(MotionEvent.ACTION_DOWN):
            balle.setX(event.getX());
            balle.setY(event.getY());
            ya = event.getY();
            xa = event.getX();
            break;

        case(MotionEvent.ACTION_UP):
            xb = event.getX();
            yb = event.getY();
            balle.setMove(true);
            break;
    }

    return true;
}

This is my moveDirection method: 这是我的moveDirection方法:

public void moveDirection(float xa, float ya, float xb, float yb) {
    if(!move) {
        return;
    }
    else {
        float m, b;
        float dx = xb-xa;

        m = (yb - ya) / (xb - xa);

        b = ya - (m * xa);

        if(dx > 0) {
            this.x += speedX;
        }
        else {
            this.x += -speedX;
        }

        this.y = (m * this.x + b);
    }
}

Thank you in advance! 先感谢您!

I see you are not capturing all the events so, maybe the code from the Android Documentation with de VelocityTracker api may help, in this example when you pull down your finger, a new tracker is created and when you move (in any direction) you capture the velocity of the event, i think you can move the ball according with the velocity and direction of touch. 我看到您没有捕获所有事件,因此,也许使用de VelocityTracker api的Android文档中的代码可能会有所帮助,在此示例中,当您放下手指时,会创建一个新的跟踪器,并且在您(向任何方向)移动时捕获事件的速度,我认为您可以根据触摸的速度和方向来移动球。

public class MainActivity extends Activity {
private static final String DEBUG_TAG = "Velocity";
    ...
private VelocityTracker mVelocityTracker = null;
@Override
public boolean onTouchEvent(MotionEvent event) {
    int index = event.getActionIndex();
    int action = event.getActionMasked();
    int pointerId = event.getPointerId(index);

    switch(action) {
        case MotionEvent.ACTION_DOWN:
            if(mVelocityTracker == null) {
                // Retrieve a new VelocityTracker object to watch the velocity of a motion.
                mVelocityTracker = VelocityTracker.obtain();
            }
            else {
                // Reset the velocity tracker back to its initial state.
                mVelocityTracker.clear();
            }
            // Add a user's movement to the tracker.
            mVelocityTracker.addMovement(event);
            break;
        case MotionEvent.ACTION_MOVE:
            mVelocityTracker.addMovement(event);
            // When you want to determine the velocity, call 
            // computeCurrentVelocity(). Then call getXVelocity() 
            // and getYVelocity() to retrieve the velocity for each pointer ID. 
            mVelocityTracker.computeCurrentVelocity(1000);
            // Log velocity of pixels per second
            // Best practice to use VelocityTrackerCompat where possible.
            Log.d("", "X velocity: " + 
                    VelocityTrackerCompat.getXVelocity(mVelocityTracker, 
                    pointerId));
            Log.d("", "Y velocity: " + 
                    VelocityTrackerCompat.getYVelocity(mVelocityTracker,
                    pointerId));
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            // Return a VelocityTracker object back to be re-used by others.
            mVelocityTracker.recycle();
            break;
    }
    return true;
}
}

Link for doc . 链接到doc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM