Search code examples
javaandroidswitch-statementjoystick

Program is entering a switch case when the requirements haven't been met


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?


Solution

  • 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;**
             }