What's wrong with this 'if/if else' statement?

edited February 2017 in Library Questions

I am using the ControLP5 library to create a list box and each item in the list box has an assigned value (0.0, 1.0, 2.0, 3.0 etc).

As you can see in the if statement below, when the user clicks on an item with a certain value, a function runs. The problem is that when any of them are clicked, the function only runs for an extremely short amount of time.

My question is, how do I get the function to run indefinitely until the next else if is activated?

I'm guessing its such an easy bit of code but I cant quite figure it out.

Please see the if statement below:

    void controlEvent(ControlEvent theEvent) {

        if (theEvent.getController().getValue() == 0.0){

           drawPathToOutpatients();
        }

        else if (theEvent.getController().getValue() == 1.0){

          drawPathToRestaurant(); 
        }

        else if (theEvent.getController().getValue() == 2.0){

          drawPathToPharmacy();
        } 

         else if (theEvent.getController().getValue() == 3.0){

          drawPathToWard1A();
        } 
    }

Please see the function that is called below:

    void drawPathToOutpatients(){

      strokeWeight (3);
      dottedLine(720, 700, 720, 580);
      dottedLine(720, 580, 1080, 580);
      dottedLine(1080, 580, 1080, 670);
      dottedLine(1080, 670, 1160, 670);
      fill (0, 153, 102);
      text ("You Are Here", 690, 730);
      ellipse(720, 700, 20, 20);
      fill(0, 102, 153);
      ellipse(1160, 670, 20, 20);
      text("Outpatients", 1130, 720);
    }
Tagged:

Answers

  • this seems to be an event :

    theEvent.getController().getValue()

    so take a closer look at the examples

    you can store the result of theEvent.getController().getValue()

    into an var and use this var from draw()

  • @Chrisir - Thank you for the reply.

    I have managed to store the result of theEvent.getController().getValue() as a float var, yet when I try to call it in draw()its says the variable does not exist.

  • Answer ✓

    then you need to define your new var before setup() so it has global scope

    or show your code

  • @Chirsir - Wow thank you so much, it has worked perfectly. I am really grateful for your help :D

    The only problem now is that the theEvent.getController().getValue() value is set at 0.0 by default when the sketch runs. Therefore the first function runs before anything is selected. Any idea's how to fix this?

  • I seem to have found a fix - I declared my new var value as 100.0 (a number that will never be selected by the user) in setup(). Then when the user clicks on any other item it will overwrite the value.

    Thanks again for your help :D

  • Answer ✓

    Well done!

Sign In or Register to comment.