【问题标题】:Processing: How do i create an object every "x" time处理:我如何每“x”时间创建一个对象
【发布时间】:2016-08-20 21:25:15
【问题描述】:

我想要做的是在我的系统中创建一个新行星,例如每 10 秒它开始移动并打印一个 "hello" 。最后,我希望这 8 个行星(椭圆)会一起移动。

我尝试使用 delay();但我失败了。

有人可以帮帮我吗?

Planet [] planetCollection = new Planet [8];
float [] wid2 = {100,200,300,400,500,600,700,800};
float [] hig2 = {50,75,100,125,150,175,200,225};
int [] colorR = {100,800,300,400,500,600,700,800};
int [] colorG = {50,225,100,125,150,175,200,225};
int [] colorB = {50,225,100,125,150,175,200,225};
int [] size =   {10,12,14,16,18,20,22,24};
int lastTime =0;
int contador =0;

void setup (){
      size (1600,1600);
      smooth();

      //INITIALIZE

      for (int i=0 ; i<planetCollection.length; i++){

         planetCollection [i] = new Planet(wid2[i], hig2[i], colorR[i], 
            colorG[i], colorB[i], size[i]);
         }

}

void draw (){
      background (0);

      //CALL FUNCIONALITY

      for (int i=0 ; i<planetCollection.length; i++){
        planetCollection [i].run();
      }
}



    class Planet {
    //GLOBAL VARIABLES
    float val;
    float x = 0;
    float y = 0;
    float wid2;
    float hig2;
    float speed;
    int colorR;
    int colorG;
    int colorB;
    int size;
    int centerx = width/2;
    int centery = height/4;


    //CONTRUCTOR
    Planet(float _w, float _h,int _colorR,int _colorG,int _colorB, int _size){

    wid2=_w;
    hig2=_h;
    colorR= _colorR;
    colorG= _colorG;
    colorB= _colorB;
    size = _size;



    speed=10/(2*PI * sqrt ((pow(wid2,2)+pow (hig2,2)/2))); ;

    }


    //FUNCTIONS

    void run (){
    move();
    display();
    }
    void move (){
      x= sin (val);
      y= cos(val);
      x *=wid2;
      y *=hig2;
      //SUN/CENTER
      noStroke();
      fill (255,238,41);
      ellipse (centerx,centery,40,40);
      if (dist (mouseX,mouseY,centerx,centery)<20){
      if(mousePressed){
      speed =0;
      }
      }
      //
      x+= centerx;
      y+= centery;
      val += speed;

      }



      void display (){
        //PLanets
      fill(colorR, colorG, colorB);
      ellipse(x, y, size, size);
      ///Orbits
      noFill();
      stroke(255);
     ellipse(centerx, centery, wid2*2, hig2*2);
      println ("posicionx "+x); 
      println ("posiciony "+y);
      println ("width "+wid2);
      println ("high "+hig2);
      println ("val "+val);
      println ("speed "+speed);


      }


    }

【问题讨论】:

  • 请缩进您的代码,以便其他人阅读
  • 你有没有想过这个问题?

标签: function class object processing


【解决方案1】:

您可以在draw() 函数中使用modulo % operatorframeCount 变量,每X 帧执行一次操作。

这是一个示例程序,它在大多数帧中绘制小圆圈,但每 60 帧绘制一个大圆圈:

void setup() {
  size(500, 500);
  background(0);
}

void draw() {

  ellipse(mouseX, mouseY, 10, 10);

  if (frameCount % 60 == 0) {
    ellipse(mouseX, mouseY, 50, 50);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-31
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 2014-03-09
    • 1970-01-01
    相关资源
    最近更新 更多