作者 主题: 新问题。。求解~为什么显示不出我想要的  (阅读 3682 次)

kunard

  • Newbie
  • *
  • 帖子: 9
新问题。。求解~为什么显示不出我想要的
« 于: 十月 28, 2011, 08:42:33 下午 »
EYE[]hieye=new EYE[100]; //定义对象

void setup() {
  size(600, 300);
  smooth();
  colorMode(HSB, 360, 100, 100);
  for (int i=0;i<hieye.length;i++) {
    hieye=new EYE(random(30,width-30),random(height),random(5,30),color(random(360),100,100));
  }
}
void draw() {
  background(255);
  for (int i=0;i<hieye.length;i++) { 
    hieye.display();
    hieye.move();
  }
}
class EYE {
  float x;
  float y;
  float d;
  float speed;
  float c;

  EYE(float x1, float y1, float d1, float c1) {
    float x=x1;
    float y=y1;
    float speed=2;
    float d=d1;
    float c=c1;
  }
  void display() {
    stroke(0);
    strokeWeight(d/15);
    fill(360);
    ellipse(x, y, d*2, d);
    strokeWeight(d/20);
    fill(c);
    ellipse(x, y, d, d);
    fill(0);
    noStroke();
    ellipse(x, y, d/3, d/3);
  }
  void move() {
    x=x+speed;
    if (x>width-d||x<d) {
      speed=-speed;
    }
  }
}
« 最后编辑时间: 十月 28, 2011, 09:32:42 下午 作者 kunard »

vinjn

  • SuperManager
  • Hero Member
  • *****
  • 帖子: 586
Re: 新问题。。求解~为什么显示不出我想要的
« 回复 #1 于: 十月 28, 2011, 09:53:24 下午 »
你还是没有理解函数的写法
这里的问题是,变量的类型不应该写在前面
否则只是创建临时的局部变量并赋值,并不会赋值给EYE中的成员变量



kunard

  • Newbie
  • *
  • 帖子: 9
Re: 新问题。。求解~为什么显示不出我想要的
« 回复 #2 于: 十月 28, 2011, 10:02:56 下午 »
谢谢大侠不厌其烦的指导!~小生有礼了!

evan

  • Newbie
  • *
  • 帖子: 28
  • HTML5/JS @ Google Creative Lab
    • personal site
Re: 新问题。。求解~为什么显示不出我想要的
« 回复 #3 于: 十月 29, 2011, 05:47:51 上午 »
如vinjn所说,EYE class的构建方法作如下修改:
程序代码
  EYE(float x1, float y1, float d1, float c1) {
    x=x1;
    y=y1;
    speed=2;
    d=d1;
    c=c1;
  }

还有就是贴代码时候最好用 [ code ] ... [ /code ] tag
不然方括号里的东西会丢失,比如 hieye[ i ] 就变成 hieye 了
« 最后编辑时间: 十月 29, 2011, 05:51:24 上午 作者 evan »
新浪微博@尤小右
twitter@youyuxi

kunard

  • Newbie
  • *
  • 帖子: 9
Re: 新问题。。求解~为什么显示不出我想要的
« 回复 #4 于: 十月 29, 2011, 06:28:43 下午 »
也谢谢大侠明示~~~嘻嘻

Tags: