Procedural to OOP
in
Programming Questions
•
1 year ago
My first attempt at converting a procedural program to OOP is not behaving as it should. I can't pinpoint what's wrong with the code, but I suspect the class I wrote is at fault somewhere. Is it the way I called/setup the mouseClicked function?
Procedural version.
- float x = 40;
- float y = 40;
- int bxW = 60;
- int bxH = 60;
- boolean rectClicked = false;
- float moveX = 2;
- int countClick = 0;
- ///////////////////////////////
- void setup() {
- size(700, 600);
- smooth();
- noStroke();
- }
- ///////////////////////////////////////
- void draw() {
- background(255);
- box1();
- emptyRect();
- }////////////////////////////// end draw
- //----------- functions -----------
- /////////////////////////////////////////
- void box1 () {
- // draw the square
- fill(#7178E5);
- noStroke();
- rect(x, y, bxW, bxH);
- if (rectClicked) {
- countClick = 1;
- x += moveX;
- if (x>= 400) {
- x = 400;
- moveX = - moveX;
- }
- if (x<=40) {
- x = 40;
- moveX = - moveX;
- }
- }
- else {
- countClick = 0;
- x = 40;
- }
- }
- ////////////////////////////////
- void emptyRect() {
- //empty rect
- stroke(#3C3D40);
- fill(255, 15);
- rect(40, 40, 60, 60);
- }
- //////////////////////////////////
- void mouseClicked() {
- box1MouseClick();
- }
- ///////- box1 mouse click functions ///////////
- void box1MouseClick() {
- if ((mouseX>42) && (mouseX<=99) && (mouseY >44) && (mouseY<99)) {
- rectClicked = true;
- }
- if ((countClick >= 1) && (mouseX>42) && (mouseX<=99) && (mouseY >44) && (mouseY<99)) {
- rectClicked = false;
- }
- }
OOP version:
- /////////globals //////////
- boolean rectClicked = false;
- int countClick = 0;
- /////////////////////////
- class Boxen {
- float x;
- float y;
- int bxW;
- int bxH;
- float speedX;
- int startX;
- int endX;
- int startY;
- int endY;
- ///////////////////////////////////
- Boxen() {
- x = 40;
- y = 40;
- bxW = 60;
- bxH = 60;
- speedX = 2;
- }
- ////////////////////////////////////
- void displayBoxen() {
- fill(#7178E5);
- noStroke();
- rect(x, y, bxW, bxH);
- }
- void moveBoxen() {
- if (rectClicked) {
- countClick = 1;
- x += speedX;
- if (x>= 400) {
- x = 400;
- speedX = - speedX;
- }
- if (x<=40) {
- x = 40;
- speedX = - speedX;
- }
- }
- else {
- countClick = 0;
- x = 40;
- }
- }
- /////////// mouseClick function ///////////
- void mouseClicked(int tempStartX, int tempEndX, int tempStartY, int tempEndY ) {
- startX = tempStartX;
- endX = tempEndX;
- startY = tempStartY;
- endY = tempEndY;
- //////////////////
- // First Square X: Parameters: 42, 99
- // 1st Square Y Parameters: 44, 99
- //
- //
- //
- //
- ////////////////////
- if ((mouseX>startX) && (mouseX<=endX) && (mouseY >startY) && (mouseY<endY)) {
- rectClicked = true;
- }
- if ((countClick >= 1) && (mouseX>startX) && (mouseX<=endX) && (mouseY >startY) && (mouseY<endY)) {
- rectClicked = false;
- }
- }
- }/// end class
- Boxen myBoxen;
- void setup() {
- size(700,600);
- background(255);
- smooth();
- noStroke();
- myBoxen = new Boxen();
- }
- void draw(){
- myBoxen.displayBoxen();
- myBoxen.mouseClicked(42,99,44,99);
- myBoxen.moveBoxen();
- emptyRect();
- }
- //////////// other function ////////////
- void emptyRect()
- {
- stroke(#3C3D40);
- fill(255, 15);
- rect(40, 40, 60, 60);
- }
1