level 6
当然可以。自定义函数都可以返回值,何况自定义的类。
除了在类里面定义一个method 来指定返回结果之外,定义的类里的参数是可以直接在void setup() 和 void draw() 里直接调用的。
2014年11月01日 17点11分
2
还是不知道怎么写,能举个例子吗。下边我写了一个程序其中有一个类,产生圆,想返回每个圆的圆心坐标(x,y),但x和y都是在类里边定义的,是局部变量吧该怎么写返回他们的值??
2014年11月02日 13点11分
level 7
int n=10;
Cricle[] cricle=new Cricle[n];
void setup(){
size(700,500);
smooth();
for(int i=0;i<n;i++){
cricle[i]=new Cricle(random(-5,5),random(-5,5),int(random(50,100)));
}
}
void draw(){
background(255);
for(int i=0;i<n;i++){
cricle[i].move();
cricle[i].display();
}
}
class Cricle{
float x,y;
float sy;
float sx;
int r;
Cricle(float tsx,float tsy,int tr){
sx=tsx;
sy=tsy;
r=tr;
}
void move(){
if((x<0)||(x>width)){
sx*=-1;
}
if((y<0)||(y>height)){
sy*=-1;
}
x+=sx;
y+=sy;
}
void display(){
fill(0,191,255);
ellipse(x,y,r,r);
}
}
2014年11月02日 13点11分
3
level 6
直接用 cricle[i].x 和 cricle[i].y 调用就行。
比如可以在void draw() 里面的cricle[i].display();后面加一句:
rect(cricle[i].x,cricle[i].y,10,10);
顺便吐槽,你的circle 拼写错了。
2014年11月02日 16点11分
4
多谢啦。拼写。。
2014年11月03日 04点11分