OOP Help
in
Programming Questions
•
2 years ago
Hey guys, NewB to Processing with an issue. I'm trying to play with OOP, but my objects aren't acting separately. Specifically they aren't moving at their own individual speeds. Thoughts? I'm hoping this is the appropriate way to post code in here:
Cube myCube1;
Cube myCube2;
void setup() {
size(1000, 1000, P3D);
myCube1 = new Cube(color(15, 15, 100, 100),500,500,0,2,0,0);
myCube2 = new Cube(color(15, 15, 100, 100),250,250,0,-2,0,0);
}
void draw() {
background(0);
myCube1.slide();
myCube1.display();
myCube2.slide();
myCube2.display();
}
class Cube {
color c;
float Xpos;
float Ypos;
float Zpos;
float Xspeed;
float Yspeed;
float Zspeed;
Cube(color tempC, float tempXpos, float tempYpos, float tempZpos, float tempXspeed, float tempYspeed, float tempZspeed) {
c = tempC;
Xpos = tempXpos;
Ypos = tempYpos;
Zpos = tempZpos;
Xspeed = tempXspeed;
Yspeed = tempYspeed;
Zspeed = tempZspeed;
}
void display() {
stroke(175);
fill(c);
translate(Xpos, Ypos, Zpos);
box(50);
}
void slide() {
Xpos = Xpos + Xspeed;
if (Xpos+25 > width) {
Xspeed = -Xspeed;
}
if (Xpos-25 < 0) {
Xspeed = -Xspeed;
}
}
}
Cube myCube1;
Cube myCube2;
void setup() {
size(1000, 1000, P3D);
myCube1 = new Cube(color(15, 15, 100, 100),500,500,0,2,0,0);
myCube2 = new Cube(color(15, 15, 100, 100),250,250,0,-2,0,0);
}
void draw() {
background(0);
myCube1.slide();
myCube1.display();
myCube2.slide();
myCube2.display();
}
class Cube {
color c;
float Xpos;
float Ypos;
float Zpos;
float Xspeed;
float Yspeed;
float Zspeed;
Cube(color tempC, float tempXpos, float tempYpos, float tempZpos, float tempXspeed, float tempYspeed, float tempZspeed) {
c = tempC;
Xpos = tempXpos;
Ypos = tempYpos;
Zpos = tempZpos;
Xspeed = tempXspeed;
Yspeed = tempYspeed;
Zspeed = tempZspeed;
}
void display() {
stroke(175);
fill(c);
translate(Xpos, Ypos, Zpos);
box(50);
}
void slide() {
Xpos = Xpos + Xspeed;
if (Xpos+25 > width) {
Xspeed = -Xspeed;
}
if (Xpos-25 < 0) {
Xspeed = -Xspeed;
}
}
}
1