Error on removing ControlP5 Button, does someone know the problem?

edited March 2016 in Library Questions

Hello everyone,

So when I press a button I want it to remove all the buttons that are drawn for the moment. For my first button this works perfectly and for the others it gives an error...

To make my code a bit clear, here there is a little explantion: So in my draw() it constantly runs the act() method from my class. It looks which gameState my game is in and depending on that it will run a piece of code (here the MainMenu). I want to switch to the "How To Play"-menu and therefor I need to remove the previously drawn buttons... It works fine when I press "Play Game" but not with the "How To Play"-button. It it because I'm removing the buttons, I found that because it doesn't give an error if I try to remove them with this button...

Can someone tell me what I'm doing wrong?

Here's my code:

        class MainMenu {    //gameState 1

          private PImage scImg;
          private boolean callMethod = true;      //used to only call a method ones

          MainMenu(PApplet thePApplet) {
            cp5 = new ControlP5(thePApplet);
          }

          public void act() {
            if(callMethod) {
              drawButtons();
              backGround();
              callMethod = false;
            }
            if(cp5.isMouseOver()){
              cursor(HAND);
            } else {
              cursor(ARROW);
            }
          }

          public void backGround() {
            scImg = loadImage("MenuScreens/startscherm.png");
            scImg.resize(breedte, hoogte);
            background(scImg);
          }

          public void drawButtons() {
            cp5.addButton("playGame")
               .setPosition(breedte*0.6, hoogte*0.05) 
               .setImage(loadImage("Buttons/PlayBut.png"))
               .updateSize()
               ;

           cp5.addButton("howToPlay")
              .setPosition(breedte*0.6, hoogte*0.12) 
              .setImage(loadImage("Buttons/HowToPlayBut.png"))
              .updateSize()
              ;

           cp5.addButton("quitGame")
              .setPosition(breedte*0.765, hoogte*0.12) 
              .setImage(loadImage("Buttons/QuitBut.png"))
              .updateSize()
              ;
          }


        } //left the class

        public void playGame() {                //runs when button with same name is clicked
          cp5.getController("playGame").remove();
          cp5.getController("howToPlay").remove();
          cp5.getController("quitGame").remove();
          gameState = 3;
        }

        public void howToPlay() {
          cp5.getController("playGame").remove();
          cp5.getController("howToPlay").remove();
          cp5.getController("quitGame").remove();
          gameState = 2;
        }

        public void quitGame() {
          exit();
        }

So when I press the "playGame" button it gives no error...

Error message:

java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0 at java.util.Vector.get(Vector.java:748) at controlP5.ControllerList.get(Unknown Source) at controlP5.ControllerGroup.setMousePressed(Unknown Source) at controlP5.ControlWindow.mouseReleasedEvent(Unknown Source) at controlP5.ControlWindow.mouseEvent(Unknown Source) at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1398) at processing.core.PApplet.handleMethods(PApplet.java:1593) at processing.core.PApplet.handleMouseEvent(PApplet.java:2679) at processing.core.PApplet.dequeueEvents(PApplet.java:2602) at processing.core.PApplet.handleDraw(PApplet.java:2413) at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1527) at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

Tagged:

Answers

  • Answer ✓

    I fixed it guys! The code was right, just did cp5 = new ControlP5(thePApplet) in 2 different classes and that removed my first cp5 instance... That's why he couldn't find the buttons

Sign In or Register to comment.