Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+2 votes

So for my final project, I plan on making a bunch of different buttons that lead to different things. I started writing my code, and I have been using the same button class for all of the separate buttons (I'm trying to get three buttons for now). When I run the code, only one rectangle draws. When I tried debugging my code, when it runs fine (I think?) but stops after I draw the first button (doesn't even consider it when I call it the other two times). It doesn't give me an error though. Does this mean I need to make a special button class for each button?

asked in CSC201 Spring 2021 by (1 point)

1 Answer

+1 vote
 
Best answer

No, you definitely should NOT make a separate class for each button!

A class is a type of thing. You should only make multiple classes if you need to have different TYPES of buttons (For example if you need an ImageButton that displays an image that you can click on and a TextButton that displays text you can click on, and a ToggleButton that you can click once to push it down and click again to pop it back up).

If you just need multiple buttons of the same type (but different text, different positions, etc), then you should just have ONE Button class, and then multiple *INSTANCES (objects) of that class.

It sounds like there's another bug going on in your code that's preventing the buttons from displaying -- have you checked:
1) are all the buttons in the same place, being drawn on top of each other?
2) are some of the buttons being drawn outside of the window boundaries (coordinates too big/small?)

Feel free to post more of your code here. Since no one is doing the same nifty project as you, there isn't any concern about people copying your code. (And if someone does want to use your Button class code, they are welcome to do so AS LONG AS THEY GIVE YOU CREDIT in the "Assistance" section in the top comment header!)

answered by (508 points)
selected by
0

Okay, that's what I figured. I just realized that I commented something out and that's why it wasn't running properly (whoops!). Finally have my three buttons, thank you for the clarification!

...