How do I keep and later use the time at which an oobject in an array list appears
in
Programming Questions
•
5 months ago
I am trying to have something happen when the difference in time from the last object in the array list appearing and the current time is 30 seconds.
here is my code
tab 1: main code
StopWatchTimer sw;
int ix = 500; //initial x
int iy = 500; // initial y
float z = 0;
int count = 0;
int delayRate = 30; // in seconds
int currentTime = 0;
int newTime = 0;
boolean start;
boolean doSpawn = true;
int mx;
PFont fontA;
PImage img;
int x2 = -524; //background x diff.
int y2 = -268; //background y diff.
PVector location;
PVector velocity;
PVector acceleration;
int locx; //
int locy; //
ArrayList places;
//ParticleSystem ps;
void setup() {
size(1000, 1000);
frameRate(30);
sw = new StopWatchTimer();
sw.start();
location = new PVector(-524, -268);
velocity = new PVector(0, 5);
acceleration = new PVector(0, 0);
img = loadImage("yellow_binary2.jpg");
fontA = loadFont("Verdana-24.vlw");
places = new ArrayList();
places.add(new Place(ix, iy));
//ps = new ParticleSystem(new PVector(width/2,50));
}
void draw() {
//Import Background/ Edit
image(img, location.x, location.y);
//location.add(velocity);
if (location.y >= 0 ) {
location.y = -268;
}
//Draw Places
for (int i = 0; i < places.size(); i++) {
Place place = (Place)places.get(i);
place.display();
if (places.size() >= 2 && i != 0) {
Place prevPlace = (Place)places.get(i-1);
line(place.x, place.y, prevPlace.x, prevPlace.y);
locx = prevPlace.x;
locy = prevPlace.y;
// println(nf(locx, 4) + ","+ nf(locy, 4));
}
}
/*for (int i = places.size()-1; i >= 0; i--) {
Place placeGone = (Place)places.get(i);
placeGone.remove();
}*/
//Screen Instructions
stroke(255);
fill(155);
rect(0, 6, 200, 25);
rect(0, 57, 200, 25);
textFont(fontA, 12);
fill(255);
text("Press Up Arrow to go", 100, 25);
text("Wait to Idle", 100, 75);
fill(255);
// Time Clock
sw.time();
//Places Rate
mx = constrain(delayRate, 0, 60);
newTime = second();
if (doSpawn) {
if (newTime % delayRate == 0 && newTime != currentTime) {
currentTime = newTime;
println("It has been " + delayRate + " seconds: " + count);
count++;
places.add(new Place());
}
}
}
//Key Controls
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
//x = 0;
location.add(velocity);
doSpawn = false;
//placeGone.remove();
}
}
if (key == 'd') {
removeMostRecentPlace();
//timeOfLastPlace();
}
}
void keyReleased() {
if (key == CODED) {
if (keyCode == UP) {
doSpawn = true;
delayRate = delayRate - 5;
currentTime = newTime;
}
if (delayRate == 0) {
delayRate = mx;
}
}
}
void removeMostRecentPlace() {
int whichOne = places.size();
places.remove(whichOne - 1);
}
// able to remove any place (dot) in the aray list such as 100)
void removThisPlace(int whichOne) {
places.remove(whichOne);
}
tab2: Place Class
class Place {
int x;
int y;
int size = 10;
color col = color(204, 153, 0);
Place() {
x = (int)random(1000);
y = (int)random(1000);
}
Place(int _x, int _y) {
x = _x;
y = _y;
}
void display() {
//println("display");
ellipse(x, y, size, size);
}
}
tab 3: stop watch timer
class StopWatchTimer {
boolean running = false;
int startTime = 0;
int stopTime = 0;
StopWatchTimer() {
}
void start() {
startTime = millis();
running = true;
}
void stop() {
stopTime = millis();
running = false;
}
int getElapsedTime() {
int elapsed;
if (running) {
elapsed = (millis() - startTime);
}
else {
elapsed = (stopTime - startTime);
}
return elapsed;
}
int second() {
return (getElapsedTime() / 1000) % 60;
}
int minute() {
return (getElapsedTime() / (1000*60)) % 60;
}
int hour() {
return (getElapsedTime() / (1000*60*60)) % 24;
}
void time() {
stroke(255);
//fill(#000000);
textAlign(CENTER);
text(nf(sw.hour(), 2)+":"+nf(sw.minute(), 2)+":"+nf(sw.second(), 2), 950, 25);
}
}
1