0

So I created a program that uses two joysticks. Each joystick is in a relative layout with the thumb of the joystick being trapped inside the layout and returning the values of the margin which I use as the X and Y values. Here is my listener program:

public boolean onTouch(View view, MotionEvent event) {
final int action = event.getAction();
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (action & MotionEvent.ACTION_MASK) {

    case MotionEvent.ACTION_DOWN: {
        RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        _xDelta = X - lParams.leftMargin;
        _yDelta = Y - lParams.topMargin;
        break;
    }

    case MotionEvent.ACTION_MOVE:{
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        leftM = X - _xDelta;
        //System.out.println("LeftM: " + leftM);
        topM = Y - _yDelta;
        //System.out.println("TopM: " + topM);
        layoutParams.rightMargin = -250;
        layoutParams.bottomMargin = -250;

                if(leftM < 0) {

                    leftM = 0;

                } else if (leftM > 220) {

                    leftM= 220;

                }

                if(topM < 0) {

                    topM = 0;

                } else if (topM > 220) {

                    topM = 220;

                }

                //Quadrant I
                if (topM < 110 && leftM > 110 ){

                    x = (int) (leftM - 110);
                    y = (int) (110 - topM);

                //Quadrant II
                } else if (topM < 110 && leftM < 110) {

                    x = (int) (leftM - 110);
                    y = (int) (110 - topM);

                //Quadrant III
                } else if ( topM > 110 && leftM < 110) {

                    x = (int) (leftM - 110);
                    y = (int) -(topM - 110);

                //Quadrant IV 
                } else if (topM > 110 && leftM > 110){

                    x = (int) (leftM - 110);
                    y = (int) -(topM - 110);

                //Origin
                } else {

                    layoutParams.leftMargin = leftM;
                    layoutParams.topMargin = topM;

                }

                if ((Math.pow(x, 2) + Math.pow(y, 2)) <= 12100) {

                    recentX = leftM;
                    recentY = topM;
                    layoutParams.topMargin = topM;
                    layoutParams.leftMargin = leftM;


                } else{

                    layoutParams.leftMargin = recentX;
                    layoutParams.topMargin = recentY;

                }

            switch (view.getId()) {

                case R.id.thumbL:
                    view.setLayoutParams(layoutParams);
                    joystick1.LeftY = (byte) (layoutParams.topMargin + 37);
                    joystick1.LeftX = (byte) (layoutParams.leftMargin + 37);
                    System.out.println("Left X Value: " + layoutParams.leftMargin + 37);
                    System.out.println("Left Y Value: " + layoutParams.topMargin + 37);

                case R.id.thumbR:
                    view.setLayoutParams(layoutParams);
                    joystick1.RightX = (byte) (layoutParams.leftMargin + 37);
                    System.out.println(" Right X Value: " + layoutParams.leftMargin +37);
             }
        break;

I had to re-map the quadrants into a normal coordinate plain, (that is the point of the if statements) then I have my switch statement. I only want each case to run if it's corresponding thumb has been pressed. Yet, if I don't have to be holding my right thumb and it will print the "Right X Value" showing me that it entered that case. Any ideas?

Shizz
  • 43
  • 6
  • Your top two cases have extra `{}` that you don't use for cases in switch statements. – jhobbie Jul 24 '14 at 18:58
  • I just added it in and it still entered the switch statement. @jhobbie – Shizz Jul 24 '14 at 19:00
  • Since we don't know what's the value of `action & MotionEvent.ACTION_MASK`, we can't say why it enters the switch case. – Eran Jul 24 '14 at 19:00
  • 1
    @Shizz you weren't supposed to add anything... you were supposed to remove `{}` from your cases. – jhobbie Jul 24 '14 at 19:04

2 Answers2

2

You could use break.

        switch (view.getId()) {

            case R.id.thumbL:
                view.setLayoutParams(layoutParams);
                joystick1.LeftY = (byte) (layoutParams.topMargin + 37);
                joystick1.LeftX = (byte) (layoutParams.leftMargin + 37);
                System.out.println("Left X Value: " + layoutParams.leftMargin + 37);
                System.out.println("Left Y Value: " + layoutParams.topMargin + 37);
                **break;**

            case R.id.thumbR:
                view.setLayoutParams(layoutParams);
                joystick1.RightX = (byte) (layoutParams.leftMargin + 37);
                System.out.println(" Right X Value: " + layoutParams.leftMargin +37);
                **break;**
         }
Mateusz
  • 2,287
  • 20
  • 28
0

If you know the screen space that is occupied by the joysticks, couldn't you check them against the X and Y coordinates?

David Carpenter
  • 1,389
  • 2
  • 16
  • 29