We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone, I am new at Processing. The question is that I have a homework and I have been given some code and asked to finished by creating a ball wich leaves a trail while moving. I have been trying for days without success. I will be really glad if someone can help me. It is supposed that from two classes I have to call a fucntion in the main program. I will leave here the code that I have been given. I hope someone can help me!! Thanks in advance!!
----------main program (dont trust in what is inside, it have been the result os my attempts)-------------------------
float x;
float y;
Trail trail = new Trail();
Trails trails = new Trails();
PVector startPoint;
void setup() {
size(500, 500);
for (int i = 0; i < trail.trailSize; i++) {
trails.Trails();
}
}
void draw() {
background(255);
ellipse(x, y, 50, 50);
if (mousePressed) {
for (int i = 0; i < trails.maxTrails; i++) {
trails.drawTrails();
}
x = mouseX;
y = mouseY;
}
}
------------------first class-----------------------------------------
class Trail {
final int DEAD = 0;
final int ALIVE = 1;
final int ZOMBIE = 2;
ArrayList<PVector> aTrail;
color trailColor;
int state;
int tnumber;
int trailSize = 1000;
int totalTrails = 0;
Trail() {
aTrail = new ArrayList<PVector>(trailSize);
trailColor = #6F480D;
state = DEAD;
tnumber = totalTrails + 1;
totalTrails++;
}
int getTrailNumber() {
return tnumber;
}
void addPointTrail(PVector p) {
if (aTrail.size() > trailSize)
aTrail.remove(0);
aTrail.add(p);
}
void setColorTrail(color c) {
trailColor = c;
}
void drawTrail() {
float shade;
if (aTrail.size() > 1) {
shade = 256.0 / aTrail.size();
for (int i=0; i < aTrail.size ()-1; i++) {
strokeWeight(10);
stroke(trailColor, shade*(i+1) );
line(aTrail.get(i).x, aTrail.get(i).y, aTrail.get(i+1).x, aTrail.get(i+1).y);
}
}
}
void reduceTrail() {
aTrail.remove(0);
if (aTrail.size() == 0) setDeadTrail();
}
void setAliveTrail() {
state = ALIVE;
}
void setDeadTrail() {
state = DEAD;
}
void setZombieTrail() {
state = ZOMBIE;
}
boolean isZombie() {
return state == ZOMBIE;
}
boolean isDead() {
return state == DEAD;
}
}
---------- and the other class given is -------------------------------
class Trails {
int maxTrails = 1000;
ArrayList<Trail> aTrails;
Trails() {
aTrails = new ArrayList<Trail>(maxTrails);
}
int newTrail(PVector startPoint, color c) {
Trail t = new Trail();
aTrails.add(t);
t.setColorTrail(c);
t.addPointTrail(startPoint);
t.setAliveTrail();
return t.getTrailNumber();
}
void drawTrails() {
Trail t;
for (int i = 0; i < aTrails.size (); i++) {
t = aTrails.get(i);
t.drawTrail();
if (t.isZombie()) t.reduceTrail();
if (t.isDead()) aTrails.remove(i);
}
}
void setZombieTrail(int tn) {
Trail t;
int i = 0;
boolean found = false;
t = aTrails.get(0);
while ( i < aTrails.size() || !found) {
t = aTrails.get(i);
found = (t.getTrailNumber() == tn);
i++;
}
t.setZombieTrail();
}
}
Answers
"trails.Trails();"
That's not how you call the constructor of a class. You should re-read your course, and perhaps the Objects tutorial.
Your draw() only draw when mouse is pressed, not sure if that's the intended effect.
[EDIT] Glanced at code again. Actually, Trails constructor is already called at declaration time. The
trail
variable is useless. In the loop, you should call newTrail() instead of Trails().Hi! Thanks for your answer. I have arrived to make a trail that follows a circle while moving. Now my problem is that I dont manage to store that trail and beging another after cicking ing another circle. It is supposed that in the class trails it must be stored. I can see it is stored but when I click in another circle it makes a line from the last point the mouse was clicked to the new instead of creating a totally new trail.
I have been reading but I havent success. Thanks!!