Getting a drawn object to "stick"
in
Programming Questions
•
1 year ago
Howdy all,
This is a little script that moves a red ball around the screen within the confines of the screen, then slows it down and stops it when you aren't moving it.
I have two questions:
1) How can I create another object where I click that isn't then immediately deleted by the background command in the draw command?
2) Why is it that when the object I'm drawing momentarily appears, instead of appearing where I'm clicking, it seems to be appearing with some relation to the ball?
Thanks for any help you can offer me, and I'd be really happy if someone could think of a way to shorten this code.
Code below:
float ballX, ballY, velX, velY = 0;- //float w=random(0,1);
- //float h=random(0,1);
- void setup(){
- size(500, 500);
- smooth();
- }
- void draw(){
- background(255);
- fill(0);
- noStroke();
- translate(ballX, ballY);
- fill(255,0,0);
- ellipse(width/2,height/2, 100,100);
- ballX = constrain(ballX,-width/2+50,width/2-50);
- ballY = constrain(ballY,-height/2+50,height/2-50);
- ballX += velX; //move ball in X direction
- ballY += velY; //move ball in Y direction
- velX *= 0.8; //this quickly slows down the ball
- velY *= 0.8; //see above
- }
- void mouseDragged() {
- ballX += mouseX - pmouseX; //drag ball in the X direction
- ballY += mouseY - pmouseY; //drag ball in the Y direction
- }
- void mouseReleased() {
- velX = mouseX - pmouseX;
- velY = mouseY - pmouseY;
- }
- void mouseClicked() {
- ellipseMode(CENTER);
- fill(0);
- ellipse(mouseX,mouseY, 15,15);
- }
1