有没有办法重新生成在屏幕上移动的云?

时间:2019-03-29 23:19:19

标签: java graphics processing

我正在整理一个在“处理”中有想法的游戏,并且我想先修复背景。

我创建了一个在后台移动的云对象数组,我希望能够在它们离开屏幕时重新生成它们。

每个云由一系列圆组成,每个圆都有自己的x位置和y偏移。他们可以在屏幕上移动确定;创建代码以重新生成它们时会出现问题。我试图创建一个if语句来检查系列中最后一个圆的x位置。

/////////////MAIN SETUP////////////////

PVector screenSize;
PVector windowPosition;
PVector windowSize;

void setup() {
  size(1400,900,P3D);
  smooth();

  screenSize = new PVector(width, height);
  windowPosition = new PVector(0,0);

  frameRate(60);

  loadEnvironment();
}

void draw() {
  background(134,224,255);
  drawEnvironment();
}

///////////////FOR THE CLOUD SETUP/////////////////


int cloudNumber;

Cloud[] totalclouds;

void loadEnvironment()
{
  cloudNumber = round(random(5,25));

  totalclouds = new Cloud[cloudNumber];
  for(int i = 0; i<cloudNumber; i++)
  {
   totalclouds[i] = new Cloud();
  }
};

///////////////////////////////////////
void drawEnvironment()
{
  for(int i = 0; i<cloudNumber; i++)
  {
    totalclouds[i].update();
  }
};

/////////////////////////////////////////
class Cloud
{
  float xPos;
  float yPos;

  float yTop;
  float yBottom;

  float xVelocityMin;
  float xVelocityMax;
  float xVelocity;

  float screenStart = 0;
  float screenEnd = width;

  int cloudSize = round(random(1,4));

  float[] xPosition = new float[cloudSize];
  float[] yPositionOffset = new float[cloudSize];
  float[] xDiam = new float[cloudSize];
  float[] yDiam = new float[cloudSize];

  float xDiameter = random(40,50);
  float yDiameter = random(40,50);
  float yOffset = random(10,20);

  float overlap = xDiameter/5;

  //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

  Cloud (){
    xPos = random(width);
    yPos = random(windowPosition.y, 350);

    xPosition[0] = xPos;
    if (xPosition.length>1)
       for(int i = 1; i<cloudSize; i++)
       {
       xPosition[i] = xPosition[i-1] + (xDiameter/2) + overlap;
       }
    for (int i = 0; i<cloudSize; i++)
    {
      yPositionOffset[i] = yOffset;
    }

    xVelocityMin = 0.3;
    xVelocityMax = 0.6;
    xVelocity = random(xVelocityMin, xVelocityMax);
  }

  //''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  void move() {
    for(int i = 0; i<cloudSize; i++)
    {
    xPosition[i] += xVelocity;
    }
  }

  //''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  void createNewClouds() {
    int lastItem = xPosition.length - 1;

    if (xPosition[lastItem]>screenEnd);
    {
      xPosition[0] = screenStart;
      if (xPosition.length>1)
       for(int i = 1; i<cloudSize; i++)
       {
       xPosition[i] = xPosition[i-1] + (xDiameter/2) + overlap;
       }
      for (int i = 0; i<cloudSize; i++)
      {
       yPositionOffset[i] = yOffset;
      }

    xVelocityMin = 0.3;
    xVelocityMax = 0.6;
    xVelocity = random(xVelocityMin, xVelocityMax);
    }
  }

  //''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  void display() {
    for(int i = 0; i<cloudSize; i++)
    {
    ellipseMode(CENTER);
    fill(255, 255, 255);
    noStroke();
    ellipse(xPosition[i], yPos + yPositionOffset[i], xDiameter, yDiameter);
    }
  }

  //''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  void update() {
    move();
    display();
    createNewClouds();
  }
}

没有再生代码,它可以正常工作-云按预期方式移动。使用该代码,云的起始位置为x=0。如果有if语句,为什么会这样?他们为什么不动呢?

0 个答案:

没有答案