Creating two toggle buttons: it looks like you're mixing active and static modes..

I get a error that dispays "it looks like you're mixing active and static modes" on Button(float xPos, float yPos, float w, float h, color onColor, color offColor).. How do I fix this?

Button(float xPos, float yPos, float w, float h, color onColor, color offColor) 
  {
    x= xPos;
    y= yPos;
    this.w= w;
    this.h= h;
    this.onColor= onColor;
    this.offColor= offColor;
    this.isPressed= false;
  }

boolean hitTest(float mx, float my)
    {
      if (mx> x && mx< x + w && my> y && my< y + h)
          {
            return true;
          }
          return false;
    } 


void drawButton ()
    {
      if(isPressed)
        {
          fill(onColor);
        }
      else
        {
          fill(offColor);
        }
      rect(x,y,w,h);
    }

  Button josh;
  Button sam;

void setup()
    {
      size(1000, 1000);
      josh= new Button(200,200,50,50,color(0,255,0),color(255,0,0));
      sam= new Button(200,400,50,50,color(0,255,0),color(255,0,0));
    }
Tagged:

Answers

  • It looks like Button is meant to be a class

    But you are missing

    class Button {

    and then before Button josh the }

    also missing the vars like x y w h ..... in the class

    Look at reference : class

    And read the tutorial on objects / oop

  • Did you copy the code maybe and forgot the beginning?

  • edited October 2015

    yeah, here's the entire code

    //Project #1.1
    //Info:
    
    //                             Toggle Button Class
    // 1. Create a class for a button control
    //        It will need to contain data for
    //          - x position                                
    //          - y position                                  
    //          - width                                       
    //          - height                                      
    //          - is pressed / is not pressed                
    //        It will need functions for                      
    //          - drawButtonControl()                        
    //          - hitTest(x,y)                                
    //        The button should change color to visually indicate when it is pressed
    // 2. Write a program that displays 2 working buttons
    
    Class Button()
      {
        float x;
        float y;
        float w;
        float h;
        boolean isPressed;
        color onColor;
        color offColor;}
    
    Button(float xPos, float yPos, float w, float h, color onColor, color offColor) 
      {
        x= xPos;
        y= yPos;
        this.w= w;
        this.h= h;
        this.onColor= onColor;
        this.offColor= offColor;
        this.isPressed= false;
      }
    
    boolean hitTest(float mx, float my)
        {
          if (mx> x && mx< x + w && my> y && my< y + h)
              {
                return true;
              }
              return false;
        } 
    
    
    void drawButton ()
        {
          if(isPressed)
            {
              fill(onColor);
            }
          else
            {
              fill(offColor);
            }
          rect(x,y,w,h);
        }
    
      Button josh;
      Button sam;
    
    void setup()
        {
          size(1000, 1000);
          josh= new Button(200,200,50,50,color(0,255,0),color(255,0,0));
          sam= new Button(200,400,50,50,color(0,255,0),color(255,0,0));
        }
    
    void draw()
    {
      background(0);
      josh.drawButton();
      sam.drawButton();
    }
    
    void mouseClicked()
      {
        if (josh.hitTest(mouseX, mouseY))
    
        josh.isPressed=!josh.isPressed;
    
    
        if(sam.hitTest(mouseX,mouseY))
        {
          josh.isPressed=!josh.isPressed;
        }
        sam.isPressed=!sam.isPressed;
     }
        }
    
  • edited October 2015

    You need to add the setup() method so you are not in immediate mode.

  • edited October 2015

    so, does it work now?

    some minor errors:

    this

    Class Button() { 
      float x; float y; 
      float w; float h; 
      boolean isPressed; color onColor; color offColor;
    
    }
    

    must be

    • Class small : class

    • no ()

    • no } here but AFTER the entire function drawButton ()

    explanation

    understand that a class is a package where you have

    class Name {
    
    many vars 
    
    constructor 
    
    many functions 
    
    }
    

    some errors in mouseClicked()

    e.g.

    the last line:

    sam.isPressed=!sam.isPressed;
    

    delete this line please.

    Chrisir ;-)

  • @quark: he has setup()

    other errors

  • His setup appears to be INSIDE the Button class :)

  • edited October 2015

    Just formatted his code - setup, draw, mouseclicked etc are in his Button class :)

  • They shouldn't be :D

  • true, but the } in line 26 is also wrong....

  • edited October 2015

    I did everything that y'all advised me to do and it fixed all errors but I also don't have any buttons, just a gray screen..

    //                             Toggle Button Class
    // 1. Create a class for a button control
    //        It will need to contain data for
    //          - x position                                  
    //          - y position                                  
    //          - width                                       
    //          - height                                      
    //          - is pressed / is not pressed                 
    //        It will need functions for                      
    //          - drawButtonControl()                         
    //          - hitTest(x,y)                        
    //        The button should change color to visually      
    //        indicate when it is pressed
    // 2. Write a program that displays 2 working buttons
    
    class Button
      {
        float x; float y;
        float w; float h;
    
        boolean isPressed;
        color onColor;
        color offColor;
    
    Button(float xpos, float ypos, float w, float h, color onColor, color offColor) 
      {
        x= xpos;
        y= ypos;
        this.w= w;
        this.h= h;
        this.onColor= onColor;
        this.offColor= offColor;
        this.isPressed= false;
      }
    
    boolean hitTest(float mx, float my)
        {
          if (mx> x && mx< x + w && my> y && my< y + h)
              {
                return true;
              }
              return false;
        } 
    
    
    void drawButton ()
        {
          if(isPressed)
            {
              fill(onColor);
            }
          else
            {
              fill(offColor);
            }
          rect(x,y,w,h);
        }
    
      Button josh;
      Button sam;
    
    void setup()
        {
          size(1000, 1000);
          josh= new Button(200,200,50,50,color(0,255,0),color(255,0,0));
          sam= new Button(200,400,50,50,color(0,255,0),color(255,0,0));
        }
    
    void draw()
    {
      background(0);
      josh.drawButton();
      sam.drawButton();
    }
    
    void mouseClicked()
      {
        if (josh.hitTest(mouseX, mouseY))
    
        josh.isPressed=!josh.isPressed;
    
        if(sam.hitTest(mouseX,mouseY))
        {
          josh.isPressed=!josh.isPressed;
        }
        //sam.isPressed=!sam.isPressed;
     }
        }
    
  • still one too many } at the very end

  • Answer ✓

    one missing } before Button josh;

  • I edited it. Is the formatting still wrong?

  • It works now!! Thank you!!!

  • Answer ✓

    this should be sam, too

    if(sam.hitTest(mouseX,mouseY))
        {
          josh.isPressed=!josh.isPressed;
        }
    
  • you're welcome

  • Yeah, I caught that and fixed it

Sign In or Register to comment.