Is it possible to use if/else if statements to check where the mouse is being pressed
in
Programming Questions
•
4 months ago
Hi,
Just recently getting into OOP, I've been experimenting with using functions - instead of having unweildy code in the body of void draw().
I have the ball changing size (ballSize) depending on what bar is pressed in the first 'if' statement. I want the ball to drop if the mouse is pressed while the ellipse/ball is being drawn around it
Snippet:
- void mousePressed() {
- //Check if the Mouse is Pressed on the Bars
- for(int i = 0; i < 5; i++) {
- if(mouseX > mySizeLevel.rectXY[i][0]
- && mouseX < mySizeLevel.rectXY[i][0] + mySizeLevel.rectW[i]
- && mouseY > mySizeLevel.rectXY[i][1]
- && mouseY < mySizeLevel.rectXY[i][1] + mySizeLevel.rectH[i]) {
- ballSize = 50-5*i;
- mySizeLevel.index = i + 1;
- println(mySizeLevel.index);
- } else if (mouseX > aimX && mouseX < aimX + aimW
- && mouseY > aimY && mouseY < aimY + aimH) {
- myballDrop.fall;
- }
- }
- }
On line 15 (above), I've referenced the function 'fall'. This is under the 'ballDrop' class, and I'm wanting it to just increment the Y-axis.
The arguments check if the mouse is being clicked within the dimensions of the rectangle the ball starts in, but I'm still getting a syntax error.
Thank you very much in advance.
Full code:
- //Class for the Size of the Ball
- sizeLevel mySizeLevel; //why does this line have to be entered?
- triangleTarget mytriangleTarget;
- ballDrop myballDrop;
- //Score Area Spec
- int scoreX = 100, scoreY = 20, scoreW = 400, scoreH = 30;
- //Aim Area Spec
- int aimX = 100, aimY = 60, aimW = 400, aimH = 70;
- //Target Area Spec
- int targetX = 100,targetY = 130, targetW = 400, targetH = 250;
- //Ball Size
- int ballX = 200, ballY = 100, ballSize = 50;
- void setup() {
- size(600,400);
- mySizeLevel = new sizeLevel(); //refer to 2nd line
- mytriangleTarget = new triangleTarget();
- myballDrop = new ballDrop();
- }
- void draw() {
- background(37,75,155);
- //Basic Background
- //Score Area - bar above where the red ball starts
- fill(255);
- rect(scoreX,scoreY,scoreW,scoreH);
- //Aim Area - where the red ball starts
- fill(255,255,0);
- rect(aimX,aimY,aimW,aimH);
- //Target Area - where the red ball will travel through
- fill(175);
- rect(targetX,targetY,targetW,targetH);
- //Ball Size
- mySizeLevel.display();
- //Display Ball
- noStroke();
- fill(255,0,0);
- ellipse(ballX,ballY,ballSize,ballSize);
- //Hit area - what the red ball needs to come in contact with to score
- mytriangleTarget.display();
- myballDrop.fall();
- }
- //This class is used to change the Size of ball
- //based on level Selection
- class sizeLevel {
- int index = 1; //Index to identify which level is current
- //Declare the Variables for 5 Bar
- int[][] rectXY = new int[5][2];
- int[] rectW = new int[5];
- int[] rectH = new int[5];
- sizeLevel() {
- //Initialize the Variable for the Size Level Bar
- //5 bars are displayed, each of increasing size
- for (int i = 0; i < 5; i++) {
- rectXY[i][0] = 20;
- rectXY[i][1] = 350-i*30;
- rectW[i] = 20+i*10;
- rectH[i] = 10;
- //ballSize = ballSize/i; will go under mousepressed instead
- }
- }
- void display() {
- //Draw the Size Level Bar
- for (int i = 0; i < 5; i++) {
- if (i < index) {
- fill(255,0,0);
- } else {
- fill(0,255,0);
- }
- rect(rectXY[i][0],rectXY[i][1], rectW[i],rectH[i]);
- }
- }
- }
- class triangleTarget {
- float hitX1=random(110,390), hitY1=380, hitX2=hitX1+30, hitY2=380, hitX3=hitX1+15, hitY3=350;
- void display() {
- //Draw the triangle target
- triangle(hitX1,hitY1,hitX2,hitY2,hitX3,hitY3);
- fill(0);
- }
- }
- class ballDrop {
- void fall() {
- ballY +=5;
- }
- }
- void mousePressed() {
- //Check if the Mouse is Pressed on the Bars
- for(int i = 0; i < 5; i++) {
- if(mouseX > mySizeLevel.rectXY[i][0]
- && mouseX < mySizeLevel.rectXY[i][0] + mySizeLevel.rectW[i]
- && mouseY > mySizeLevel.rectXY[i][1]
- && mouseY < mySizeLevel.rectXY[i][1] + mySizeLevel.rectH[i]) {
- ballSize = 50-5*i;
- mySizeLevel.index = i + 1;
- println(mySizeLevel.index);
- } else if (mouseX > aimX && mouseX < aimX + aimW
- && mouseY > aimY && mouseY < aimY + aimH) {
- myballDrop.fall;
- }
- }
- }
- void mouseMoved() {
- if (mouseX > aimX && mouseX < aimX + aimW
- && mouseY > aimY && mouseY < aimY + aimH) {
- ballX = mouseX;
- ballY = mouseY;
- }
- }
1