My buttons stop working;

edited December 2017 in Questions about Code

OK, I'm making a space invaders game & ran into a problem. I been slamming into a wall for 15 minutes then decided I'll ask others. So I got buttons, Button[] button, leading to different rooms, float room, it works fine until I return to my menu room then the buttons stop working. I click on them & nothing. There are 3 buttons in the menu. 5 buttons in all, the other 2 buttons are in other rooms. I hope I gave all the information you need. Processing Java. When the problem happened I was coming back from looking at my high score board with a menu button.

   float wave, alive, dead, boss,room,high;
    int checks;
    ArrayList<Enemy> enemy;
    ArrayList<Bullet> bullet;
    boolean restart = false;
    Score[] score = new Score[5];
    Button[] button = new Button[5];
    Ship ship;
    HighScore highscore;
    LVL lvl;
    Reset reset;
    Movement movement;

    void setup(){
    size(500,600);
    reset = new Reset();
    enemy = new ArrayList<Enemy>();
    lvl = new LVL();
    for (int i = 0 ; i < 5 ; i++) {
      score[i] = new Score();
    }
    highscore = new HighScore();
    button[0] = new Button(width/2,100,200,50);
    button[1] = new Button(width/2,160,200,50);
    button[2] = new Button(width/2,220,200,50);
    button[3] = new Button(width/2,100,200,50);
    button[4] = new Button(width-125,30,200,50);
    ship = new Ship(width/2,height-15,10);
    bullet = new ArrayList<Bullet>();
    movement = new Movement();
    }

    void draw(){
    background(0);
    if (restart == true){
      reset.update();
      button[3].show();
      button[3].update();
      button[1].show();
      button[1].update();
      text("Replay", button[3].x-50,button[3].y+10);
      text("High Score", button[1].x-75,button[1].y+10);
    }
    if (room == 0){
    for (int i = 0; i < 3; i++){
    button[i].show();
    button[i].update();
    text("Start Game", button[0].x-90,button[0].y+10);
    text("High Score", button[1].x-75,button[1].y+10);
    text("Movement", button[2].x-75,button[2].y+10);
    }
    }
    if (room == 1){
      lvl.show();
      lvl.make();
      lvl.update();
    }
    if (room == 2){
      highscore.show();
    for (int i = 0; i<5; i++){
      textSize(32);
      fill(255);
      text(i+1 + " High Score: " + score[i].total, 10, 150+(50*i));
    }
    }
    if (room == 3){
    movement.show();
    }
    }

    void mousePressed(){
      if (mouseButton == LEFT && button[0].collide == true){
        room = 1;
      }
      if (mouseButton == LEFT && button[1].collide == true){
        room = 2;
        if (restart == true){
          highscore.update();
          highscore.on = false;
          restart = false;
        }
      }
      if (mouseButton == LEFT && button[2].collide == true){
        room = 3;
      }
      if (mouseButton == LEFT && button[4].collide == true){
        room = 0;
      }
    }

class Button{
      float x,y,w,h;
      boolean collide = false;
      Button(float x, float y, float w, float h){
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
      }
      void show(){
        fill(255,255,0);
        rectMode(CENTER);
        rect(x,y,w,h);
        fill(255,0,0);
        textSize(32);
      }
      void update(){
        if (mouseX < x+w/2 && mouseX > x-w/2 && mouseY < y+h/2 && mouseY > y-h/2){
          collide = true;
        }
      }
    }
Tagged:

Answers

  • Remark

    first you want to use ctrl-t more often in processing, auto-format indents

    Remark

    then in draw, you want to use if... else if.... throughout (change every if except the first one to else if)

    Remark

    you want to use if... else if.... in mousePressed too :

     if (room == 0) {
    
     else  if (room == 1) {
    
    else  if (room == 2) {
    

    etc., so you test only the buttons that are allowed in this room.

    Remark

    This

    _ if (restart == true) {
          highscore.update();
          highscore.on = false;
          restart = false;
        }
    

    belongs into the section in draw, I'd say

     if (restart == true) {
    
  • Answer ✓

    i found the issue it was in the button the collide was staying true oce I got rid of that it works fine.

  • My points still apply ;-)

Sign In or Register to comment.