Question

how can I limit the Touchevent to one/ two time(s), that means the user can only do one time a touchevent.

Here is the code from the Touchevent, I dont know how I should edit it, that the touchevent only react one or two times.

   @Override
 public boolean onTouchEvent(MotionEvent event) {
    float currentX = event.getX();
    float currentY = event.getY();
    float deltaX, deltaY;
    float scalingFactor = 15.0f / ((box.xMax > box.yMax) ? box.yMax : box.xMax);
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    deltaX = currentX - previousX; //vorherig(previous)  - aktuell
          deltaY = currentY - previousY;
          ball.speedX += deltaX * scalingFactor;
          ball.speedY += deltaY * scalingFactor;
      break;              
    case MotionEvent.ACTION_UP:
        // Modify rotational angles according to movement

    }
    // Save current x, y
    previousX = currentX;
    previousY = currentY;
    return true;  // Event handled
 }
Was it helpful?

Solution

Maintain an boolean variable as below...

boolean touchCounter = false;

@Override
 public boolean onTouchEvent(MotionEvent event) {
    float currentX = event.getX();
    float currentY = event.getY();
    float deltaX, deltaY;
    float scalingFactor = 15.0f / ((box.xMax > box.yMax) ? box.yMax : box.xMax);

    if (touchCounter == false) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

             touchCounter = true;

             deltaX = currentX - previousX; //vorherig(previous)  - aktuell
             deltaY = currentY - previousY;
             ball.speedX += deltaX * scalingFactor;
             ball.speedY += deltaY * scalingFactor;
             break;              
        case MotionEvent.ACTION_UP:

             touchCounter = true;

             // Modify rotational angles according to movement
             break;

        }
    }

    // Save current x, y
    previousX = currentX;
    previousY = currentY;
    return true;  // Event handled
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top