Using the append() from within classes
in
Programming Questions
•
2 years ago
I 'have been spending the last days trying to solve this, I have a "ball" that circulate on the screen. When the ball is "offScreen", the ball's variables should be aded to another array (BouletteFixe) that will be displayed later on. Then, another ball will be circulated on a different direction and should be saved also.
I use append() in every manner (following Mrs Reas&Fry book aswell Mr Schiefman book). I feel stupid because the answer is probably obvious.
here is the code :
// Boulettes
// avancer et garder la derniere
Boulette blt;
BouletteFixe[] bltfx = new BouletteFixe[1];
// avancer et garder la derniere
Boulette blt;
BouletteFixe[] bltfx = new BouletteFixe[1];
Boolean offScreen = false ;
float v_xpos, v_ypos,v_diam ;
color v_c ;
float v_xpos, v_ypos,v_diam ;
color v_c ;
void setup() {
size(400, 400, P3D);
blt = new Boulette();
bltfx[0] = new BouletteFixe(0.0,0.0,0.0,color(0));
}
size(400, 400, P3D);
blt = new Boulette();
bltfx[0] = new BouletteFixe(0.0,0.0,0.0,color(0));
}
void draw() {
background(255);
frameRate(5);
for (int i=0; i<bltfx.length ; i++) {
bltfx[i].display();
}
if (offScreen != true) {
blt.move();
}else {
/// PROBLEMS
BouletteFixe[] boul = new BouletteFixe(v_xpos,v_ypos,v_diam,v_c);
bltfx =(BouletteFixe[])append(BouletteFixe,boul);
// je mets la nouvelle boulette à la fin
offScreen = true ;
}
blt.display();
}
//===================================
class Boulette {
float []xpos = new float [1];
float []ypos = new float [1];
float []diam = new float [1];
color []c = new color [1];
float theta ;
background(255);
frameRate(5);
for (int i=0; i<bltfx.length ; i++) {
bltfx[i].display();
}
if (offScreen != true) {
blt.move();
}else {
/// PROBLEMS
BouletteFixe[] boul = new BouletteFixe(v_xpos,v_ypos,v_diam,v_c);
bltfx =(BouletteFixe[])append(BouletteFixe,boul);
// je mets la nouvelle boulette à la fin
offScreen = true ;
}
blt.display();
}
//===================================
class Boulette {
float []xpos = new float [1];
float []ypos = new float [1];
float []diam = new float [1];
color []c = new color [1];
float theta ;
Boulette() {
for (int i=0; i<xpos.length; i++) {
xpos[i] = width/2;
ypos[i]= height/2;
diam [i]= 10.0 ;
c [i] = 100 ;
}
theta = +2 ;
}
for (int i=0; i<xpos.length; i++) {
xpos[i] = width/2;
ypos[i]= height/2;
diam [i]= 10.0 ;
c [i] = 100 ;
}
theta = +2 ;
}
void move() {
for (int i = 0; i < xpos.length-1 ; i ++) {
xpos[i] = xpos[i+1] ;
ypos[i] = ypos[i+1] ;
}
// si je suis offScreen
if (xpos[xpos.length-1]+15>width || ypos[ypos.length-1]<0) {
v_xpos=xpos[xpos.length-1];
v_ypos=ypos[xpos.length-1];
v_diam=diam[xpos.length-1];
v_c = c[xpos.length-1] ;
offScreen = true ;
}
else {//nouvelles valeurs
xpos[xpos.length-1] = xpos[xpos.length-1]+15;
ypos[ypos.length-1] = ypos[ypos.length-1]-theta ;
}
}
for (int i = 0; i < xpos.length-1 ; i ++) {
xpos[i] = xpos[i+1] ;
ypos[i] = ypos[i+1] ;
}
// si je suis offScreen
if (xpos[xpos.length-1]+15>width || ypos[ypos.length-1]<0) {
v_xpos=xpos[xpos.length-1];
v_ypos=ypos[xpos.length-1];
v_diam=diam[xpos.length-1];
v_c = c[xpos.length-1] ;
offScreen = true ;
}
else {//nouvelles valeurs
xpos[xpos.length-1] = xpos[xpos.length-1]+15;
ypos[ypos.length-1] = ypos[ypos.length-1]-theta ;
}
}
void display() {
//translate(width/2, height/2) ;
for (int i = 0 ; i < xpos.length ; i ++) {
// println("X "+xpos[i]);
// println(ypos[i]);
// println(offScreen);
fill(100);
stroke(0);
ellipseMode(CENTER);
ellipse(xpos[i], ypos[i], diam[i], diam[i]);
}
}
}
//==================================================================
//translate(width/2, height/2) ;
for (int i = 0 ; i < xpos.length ; i ++) {
// println("X "+xpos[i]);
// println(ypos[i]);
// println(offScreen);
fill(100);
stroke(0);
ellipseMode(CENTER);
ellipse(xpos[i], ypos[i], diam[i], diam[i]);
}
}
}
//==================================================================
// class BouletteFixe
class BouletteFixe {
class BouletteFixe {
float xpos ;
float ypos ;
float diam ;
color c ;
float ypos ;
float diam ;
color c ;
BouletteFixe (float v_xpos, float v_ypos,float v_diam,color v_c) {
}
}
void display() {
fill(100);
stroke(0);
ellipseMode(CENTER);
for (int i=0; i<bltfx.length; i++) {
// ellipse(xpos[i], ypos[i], diam[i], diam[i]);
ellipse(xpos, ypos, diam, diam);
}
}
// void update() {
// }
}// fin boulfixe
fill(100);
stroke(0);
ellipseMode(CENTER);
for (int i=0; i<bltfx.length; i++) {
// ellipse(xpos[i], ypos[i], diam[i], diam[i]);
ellipse(xpos, ypos, diam, diam);
}
}
// void update() {
// }
}// fin boulfixe
1