0

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
 }
mexxdroid
  • 5
  • 2

1 Answers1

1

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
 }
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • 2
    How can you increment `boolean`? I mean what do you mean by `touchCounter++` where `touchCounter` is a `boolean`? – Gopal Gopi Mar 08 '14 at 07:54
  • thank you very much hamid it run well! :) and what should I change, when the user have 2 touchevents and not only one? – mexxdroid Mar 08 '14 at 10:10
  • then you should maintain it using a `int` type counter variable...and apply condition based on that counter...if counter<2 then it should allow to continue touchevent or not. – Hamid Shatu Mar 08 '14 at 10:19
  • I change boolean to int, wrote if (touchCounter <2) and change all true and false to 2, but the touchcounter in action_up to 1. where is my mistake? – mexxdroid Mar 08 '14 at 12:57
  • you look like you know much in android, how can I stop the ball, I think the ball must stop in action_up, but I dont know how I should write it, I try ball.speedx-- and ballspeedy--. And the ball should stop from the devices not from the user. :) – mexxdroid Mar 08 '14 at 13:07