Hi, I am working on a project and am relatively new to programming. I am have setup a ball class outside of main tab and am having trouble with using mouse events inside the ball class which are then called in the draw() function.
I am hoping for some advice on how I can start the ball on the grey box and when the mousePressed a line is drawn to show trajectory. When mouseReleased I want the ball to launch in the opposite direction.
Currently The ball is following the mouse and I don’t know how to implement a mouseReleased into the ball class and call it without looping continuously in draw() function. Any help is much appreciated.
/* ------------------------ Main Tab ----------------------*/
void setup()
{
size(450,650);
background(0);
stroke(0);
ball = new Ball(20.0);
//slider = new TBar(200);
}
Ball ball;
//TBar slider;
void draw()
{
background(0);
playingArea();
ball.display();
ball.moveBall();
ball.edgeDetection();
ball.mouseReleased();
//slider.display();
//slider.update();
}
void playingArea()
{
//centre the all shapes so x and y coord can be easily configured.
rectMode(CENTER); // Set rectMode to CENTER
//green
fill(0,193,66);
rect(width/2,height*0.5, 400,600);
fill(150);
//teeBox
rect(width/2,height*0.9, 125,40);
// hole
fill(0);
ellipse(width/2, height*0.1, 20,20);
}
/*----------------------- Ball Class --------------------------------*/
public class Ball
{
private float xPos;
private float yPos;
private float diameter;
private float xSpeed = 0.0;
private float ySpeed = 0.0;
private float friction = 0.996;
//Constructor method
public Ball(float diameter)
{
setDiameter(diameter);
}
//Draw the ball on the display window
public void display()
{
fill(255);
strokeWeight(1);
ellipse(xPos, yPos, diameter, diameter);
}
public void moveBall()
{
xPos = xPos + xSpeed;
yPos = yPos + ySpeed;
//add friction to slow the ball down after shot is taken
xSpeed = xSpeed*friction;
ySpeed = ySpeed*friction;
if(mousePressed == true)
{
strokeWeight(2);
line(xPos,yPos,mouseX,mouseY);
}
}
public void mouseReleased()
{
xSpeed = (mouseX - xPos);
ySpeed = (mouseY - yPos);
//noLoop();
}
public void edgeDetection()
{
if (xPos > width-30-diameter/2 || xPos < 30+diameter/2) {
xSpeed *= -1.0;
}
if (yPos > height-30-diameter/2 || yPos < 30+diameter/2) {
ySpeed *= -1.0;
}
}
//getter methods
public float getXPos()
{
return xPos;
}
public float getYPos()
{
return yPos;
}
public float getDiameter()
{
return diameter;
}
//setter methods
public void setDiameter(float diameter)
{
//The ball diameter must be between 20 and height/6 (inclusive)
if ((diameter >= 20) && (diameter <= height/6)){
this.diameter = diameter;
}
else {
// If an invalid diameter is passed as a parameter, a default of 20 is imposed.
// With this animation, if we do not supply a default value for the diameter, a ball may not be
// drawn on the display window. Important note: it is not always appropriate to provide a default
// value at mutator level; this will depend on your design.
this.diameter = 20;
}
}
}