我在處理某些對象時遇到了問題。代碼應該有兩個對象顯示和移動。但我只看到一個對象顯示和移動。也許有一些我錯過了。檢查代碼。從構造函數傳遞參數到處理中的函數/ java
Rule myRule;
Rule myRule1;
void setup() {
size(200,200);
smooth();
//Initialize rule objects
myRule = new Rule(0,100,1);
myRule1 = new Rule(0,140,20);
}
void draw() {
background(255);
//int x1 = 0;
//int y1 = 0;
//Operate Rule object
myRule.move();
myRule.display();
myRule1.move();
myRule1.display();
}
class Rule {
float x;
float y;
float spacing;
float speed;
Rule(float x1, float y1, float s1) {
x = x1;
y = y1;
spacing = 10;
speed = s1;
}
void move() {
x = x + speed;
if((x > width) || (x < 0)) {
speed = speed * -1;
}
}
//Display lines at x location
void display() {
stroke(0);
fill(175);
line(x, height/2, width/2, height/2);
}
}
代碼本身對我來說看起來很好。你在使用哪個框架? – 2010-08-13 18:33:12
有一些代碼丟失,可能有問題。 – 2010-08-13 19:50:29