문제

I am trying to provide a communication method using Arduino/Wiring to make two objects move with two joysticks.

This is the method that I use for the communication:

public PVector serialEvent(Serial myPort) {
    PVector direction = new PVector(0, 0);
    PVector directionD = new PVector(0, 0);

    // read the serial buffer:
    String myString = myPort.readStringUntil('\n');
    if (myString != null) {
        String [] dataJoystick1 = split(myString, ",");
        // println (dataJoystick);
        try {
            direction.set(Float.parseFloat(dataJoystick1[0]), Float.parseFloat(dataJoystick1[1]), 0);
            directionD.set(Float.parseFloat(dataJoystick1[2]), Float.parseFloat(dataJoystick1[3]), 0);
        }
        catch(NumberFormatException e) {
        }

        movimiento=direction;
        movimientoD=directionD;

        //==============================PJ1========
        if (direction.x==0) {
            movimiento.set(0, movimiento.y, 0);
        }

        if (direction.y==0) {
            movimiento.set(movimiento.x, 0, 0);
        }
    }

    //==============================PJ2========
    if (directionD.x==0) {
        movimiento.set(0, movimiento.y, 0);
    }

    if (directionD.y==0) {
        movimiento.set(movimiento.x, 0, 0);
    }

    return direction;
    return directionD;
}

As you can see, I have two PVectors for each character direction and directionD. However, when I run the code it gives me this error:

Unreachable code in the line return directionD;

도움이 되었습니까?

해결책

return direction;
return directionD;

You cannot have two return statements like this without a condition. When the first return statement is executed, there is no chance to reach the second return statement. That's why there is an unreachable statement.

다른 팁

@vishal_aim is correct.

The way you'd have two different variables affected is creating two instances of either the PVector class or a wrapper (character) class that serialEvent can see and then passing values you receive from the serialEvent into the two instances.

Also, I'm not totally sure but I don't think you can make serialEvent return a value as it is attached void serialEvent is called from within the serial module of Processing.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top