Oop 如何在处理过程中单独控制以编程方式创建的对象的实例?
在处理过程中,我试图创建一个显示x数量形状的可视化。由于数量可能相当大且不确定,因此我希望以编程方式(使用循环)创建它们。这看起来像这样:Oop 如何在处理过程中单独控制以编程方式创建的对象的实例?,oop,object,processing,Oop,Object,Processing,在处理过程中,我试图创建一个显示x数量形状的可视化。由于数量可能相当大且不确定,因此我希望以编程方式(使用循环)创建它们。这看起来像这样: First firstDot1; float offset; float radius = 0; float endRad = 100; float speed = 0.1; String[] lines,colors; void setup(){ size(800,600); smooth(); background(255);
First firstDot1;
float offset;
float radius = 0;
float endRad = 100;
float speed = 0.1;
String[] lines,colors;
void setup(){
size(800,600);
smooth();
background(255);
firstDot1 = new First(5);
}
void draw(){
background(255);
for(int z=0; z<36; z++){
offset = z * 10;
firstDot1.display();
firstDot1.start(offset);
}
}
class First{
float angle;
int id;
float eRad = 5;
float xpos, ypos, rad, i;
Boolean start = true;
First(float tempAngle){
angle = tempAngle;
}
void display(){
noStroke();
fill(247,147,30);
ellipseMode(CENTER);
ellipse(xpos, ypos, eRad, eRad);
}
void start(float offset){
if(i<endRad){
i = i+speed/2;
xpos = width/2 + cos(radians(-angle+offset))*(radius+i);
ypos = height/2 + sin(radians(-angle+offset))*(radius+i);
}
else{
turn(offset);
}
}
void turn(float offset){
angle = angle-speed/50;
xpos = width/2 + cos(radians(-angle+offset))*(endRad);
ypos = height/2 + sin(radians(-angle+offset))*(endRad);
}
}
First firstDot1;
浮动偏移量;
浮动半径=0;
浮点数endRad=100;
浮动速度=0.1;
字符串[]行、颜色;
无效设置(){
规模(800600);
光滑的();
背景(255);
firstDot1=新的第一个(5);
}
作废提款(){
背景(255);
对于(int z=0;z,您需要创建第一个对象的数组:
First[] dots = new First[36];//declare and initialize an array of First object with size/length 36
然后需要初始化数组中的对象:
for(int i = 0 ; i < dots.length; i++) dots[i] = new First(5);
另外,请查看示例>主题>GUI>按钮非常感谢。我一直在玩数组的概念,但没有弄明白。您的答案很好,感谢您的努力!不用担心。使用单独的真正基本的草图来测试新事物(如数组或原语、对象数组)可能会更容易,然后编写更复杂的草图。很高兴答案有帮助:)
First[] dots;
float offset;
float radius = 0;
float endRad = 100;
float speed = 0.1;
String[] lines,colors;
void setup(){
size(800,600);
smooth();
background(255);
dots = new First[36];
for(int i = 0 ; i < dots.length; i++) dots[i] = new First(5);
}
void draw(){
background(255);
for(int z=0; z<36; z++){
offset = z * 10;
dots[z].update(mouseX,mouseY,mousePressed);
dots[z].display();
dots[z].start(offset);
}
}
class First{
float angle;
int id;
float eRad = 5;
float xpos, ypos, rad, i;
Boolean start = true;
color up = color(247,147,30);
color over = color(0);
color down = color(217,117,0);
boolean isOver,isDown;
First(float tempAngle){
angle = tempAngle;
}
void update(int mx,int my,boolean pressed){
isOver = (dist(mx,my,xpos,ypos) < eRad);
isDown = pressed;
}
void display(){
noStroke();
fill(up);//default
if(isOver) fill(over);
if(isOver && isDown) fill(down);
ellipseMode(CENTER);
ellipse(xpos, ypos, eRad, eRad);
}
void start(float offset){
if(i<endRad){
i = i+speed/2;
xpos = width/2 + cos(radians(-angle+offset))*(radius+i);
ypos = height/2 + sin(radians(-angle+offset))*(radius+i);
}
else{
turn(offset);
}
}
void turn(float offset){
angle = angle-speed/50;
xpos = width/2 + cos(radians(-angle+offset))*(endRad);
ypos = height/2 + sin(radians(-angle+offset))*(endRad);
}
}