我对处理有些陌生,我正在尝试制作一个鼠标按下事件,它会在屏幕上创建旋转的立方体。下一阶段将是在它们之间创建碰撞事件,但现在我遇到了另一个问题:它有效,但只有在按下鼠标时才有效,而我希望它们即使没有按下鼠标也能持续下去。
这是代码,我尝试了一个变通方法,用无限的for循环,它产生了一个奇怪的小故障,所有这些旋转的立方体都在旋转,直到变成0,0…实际上无效绘制是空的,因为一切都在被擦除。
int pointmousex;
int pointmousey;
float a=0;
float r=0;
float co=100;
float Cubox[]= {
};
float Cuboy[]= {
};
float Cuboz[]= {
};
boolean bgON=true;
void setup() {
size(800, 640, P3D);
colorMode(HSB);
background(0);
smooth();
}
void draw_box(float x, float y, float box_size, float rot) {
float z=box_size;
translate(x, y, z);
rotateX(rot);
rotateY(rot);
fill(map(co, 0, 255, 100, 255));
box(box_size, box_size, box_size);
}
void draw() {
if (bgON==true) {
fill(255, 230, 200);
rect(0, 0, width, height);
}
r=r+0.1;
}
void mousePressed() {
int pointmousex = mouseX;
int pointmousey = mouseY;
lights();
draw_box(pointmousex, pointmousey, 100, r*0.2);
}
void keyPressed() {
if (key=='b' || key=='B') {
if (bgON==true) {
bgON = false;
} else {
bgON = true;
}
}
}
如果你想用盒子做更复杂的事情,我建议你创建类:
class Box{
float x,y,z, box_size, rot;
//create instance of box with specific position and starting rotation
Box(float x, float y, float box_size, float rot){
this.z = box_size;
this.x = x;
this.y = y;
this.box_size = box_size;
this.rot = rot;
}
void draw_box(){
pushMatrix();
translate(x, y, z);
//adding R will rotate all boxes
rotateX(r + rot);
rotateY(r + rot);
fill(map(co, 0, 255, 100, 255));
box(box_size);
popMatrix();
}
//here you can add more complex methods like colision ...
}
然后你必须创建全局数组来存储这些盒子
final static ArrayList<Box> boxes = new ArrayList();
在鼠标事件中只需创建新框并将其添加到框
void mousePressed() {
boxes.add( new Box(mouseX, mouseY, 100, r*0.5));
}
您需要做的最后一件事是每次调用Draw()
时重新绘制场景(每个框):
void draw() {
background(255,230,200);
for(Box box: boxes){
box.draw_box();
}
r=r+0.01;
}