Question

I have a problem with Touch Events which i am sure is possible.

Lets say i have a Cube when i Swipe the screen left or right i want the Cube to move the same and then when i do a quick touch (Down) i want the cube to rotate by a certain amount.

Currently if i swipe the screen then the Cube will move left or right as i want but it will also rotate once.

Here is my code.

public boolean onTouchEvent(MotionEvent event)
{
    if (event != null)
    {
        float x = event.getX();
        //float y = event.getY();

        if (event.getAction() == MotionEvent.ACTION_MOVE)
        {
            if (mRenderer != null)
            {
                float CubeX = (x - mPreviousX) / mDensity / 2f;  
                mRenderer.mCubeXMovement += CubeX;
            }
        }           
        else if (event.getAction() == MotionEvent.ACTION_DOWN)
        {
            if (mRenderer != null)
            {
                float deltaX = 45f;
                mRenderer.mDeltaX += deltaX;
            }
        }

        mPreviousX = x;
        mPreviousY = y;

        return true;
    }
    else
    {
        return super.onTouchEvent(event);
    }
}

The above code only looks for the Down event which is why it rotates and moves on any type of touch How could i implement this so that the cube will only rotate when a down and up event are sequential

Was it helpful?

Solution

it's either DOWN [ -> MOVE ] -> UP or DOWN [ -> MOVE ] -> CANCEL

but ACTION_UP always follows ACTION_DOWN

to check for a quick touch, mark the down location (x&y) and then make sure that the UP location is within TOUCH_SLOP (ViewConfiguration.getScaledTouchSlop()) of the down location.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top