Deleting Spheres over time
in
Programming Questions
•
4 months ago
Heres the entire code:
//SPHRE PROGRAM MADE BY MILES JORDAN
// This program is used to creat sphere objects that drop and bounce on the floor.
// The lighting of the spheres change color when you press ENTER (RETURN if you are on a Mac).
// You must make a ball with the mouse to change the color fist, so that the computer keyboard may recognise you are on the program.
// The green, directional lights slightly change according to where the cursor is.
// The program now can increase the size of the spheres
// Pressing the R key can now generate spheres at random areas
ArrayList<Ball> balls = new ArrayList<Ball>();
float gravity = 0.1;
//arrays are used to store objects, and an array list can be used to make multiple objects, as it can increase better then a normal array
float lightvalue = 0;
//the light value will be used to change the light type, 0 for regular lights function, 1 for directional, 2 for ambient, 3 for spot light. To change the light, press Enter
float ballsize = 10;
//the size of the ball will increase from 1-0
float rX;
float rY;
// two variables specifying random values in the x and y value. rX is x value and rY is y value
PFont f;
void setup() {
size(1500, 700, P3D);
// start with one ball
balls.add( new Ball(width/2, 0, 0)); //
background(0);
frameRate(60);
f = createFont("Arial",16,true); // Arial, 16 point, anti-aliasing on
}
//The key pressing here is used to try and change the the lightvalue, to later change the lights
void keyPressed() {
if (key == ENTER || key == RETURN) {
lightvalue = lightvalue + 1;
}
if (lightvalue > 3) {
lightvalue = 0;
}
if (key == '1') {
ballsize = 10;
}
if (key == '2') {
ballsize = 20;
}
if (key == '3') {
ballsize = 30;
}
if (key == '4') {
ballsize = 40;
}
if (key == '5') {
ballsize = 50;
}
if (key == '6') {
ballsize = 60;
}
if (key == '7') {
ballsize = 70;
}
if (key == '8') {
ballsize = 80;
}
if (key == '9') {
ballsize = 90;
}
if (key == '0') {
ballsize = 100;
}
if (key == 'r' || key == 'R' ) {
rX = random(width);
rY = random(height);
balls.add( new Ball(rX, rY, 0));
}
}
void draw() {
background(0);
textFont(f,24);
text ( "Welcome to the Sphere Simulator!" ,0,24);
text ( "Click on the screen to generate a sphere!" ,0,48);
text ( "Press the keys 1 through 0 to increase the size!" ,0,72);
text ( "Press the R key to generate a sphere randomly!" ,0,96);
text ( "Press Enter/Return to change the lights!" ,0,120);
//println(mouseY);
if (lightvalue == 0) {
lights();
}
if (lightvalue == 1) {
ambientLight(0,0,255);
}
if (lightvalue == 2) {
directionalLight(0, 255, 0, mouseX, mouseY, 0);
}
if (lightvalue == 3) {
spotLight(255, 0, 0, width/2, height/2, 400, 0, 0, -1, PI/2, 2);
}
// Ball is the datatypye of the stuff in the Array, b is the temporary name for each element in the array, balls is the name of the array to iterate through
for (Ball b:balls) { //update() and dislay() are methods, in which b is using, and the dot "." connects the two, allowing b to use the two methods
b.update(); //update is used to improve the falling of the balls
b.display(); //display is used to make the balls, constantly
}
//gonna try and use frameRate and remove() to remove objects from the array
}
void mousePressed() {
balls.add(new Ball( mouseX, mouseY, 0));
//this adds the object
}
class Ball {
float x;
float y;
float z;
float speed;
// a constructor:
Ball(float _x, float _y, float _z) {
x = _x;
y = _y;
z = _z;
speed = 0;
}
void display() {
noStroke();
pushMatrix();
translate(x, y, z);
sphere(ballsize);
popMatrix();
}
void update() {
y = y + speed;
speed = speed + gravity;
if (y > height) {
speed = speed * -0.65;
}
}
}
Now what im trying to do is over time, get the sphere objects I create to be deleted over time. Lets say, for each ball I create, I want each one of them to last 10 seconds, and then be deleted. How do I go about this?
1