create new object with mouse click
in
Programming Questions
•
1 month ago
how do i add new ball object with every mouse click?
- Ball myBall;
- void setup() {
- size(600, 600);
- smooth();
- myBall=new Ball(random(width), random(height));
- }
- void draw() {
- background(0);
- myBall.run();
- }
- //BALL CLASS
- class Ball {
- float x, y;
- float speedX=20;
- float speedY=5;
- Ball(float _x, float _y) {
- x=_x;
- y=_y;
- }
- void run() {
- display ();
- move();
- bounce();
- gravity();
- }
- void gravity() {
- speedY+=0.2;
- }
- void bounce() {
- if (x>width) {
- speedX=speedX*-1;
- }
- if (x<0) {
- speedX=speedX*-1;
- }
- if (y>height) {
- speedY=speedY*-1;
- }
- if (y<0) {
- speedY=speedY*-1;
- }
- }
- void move() {
- x=x+speedX;
- y=y+speedY;
- }
- void display() {
- ellipse(x, y, 20, 20);
- }
- }
1