Processing 在使用类进行处理时尝试使球在屏幕上反弹

Processing 在使用类进行处理时尝试使球在屏幕上反弹,processing,Processing,我正在努力使单个球在整个屏幕上反弹,但我在使球反弹方面有问题bounce()函数负责使球反弹,但我遇到了数组越界异常,尽管只通过数组中的1个元素。我提供了下面的代码。此代码使用2个类(Ball\u用法和Ball) Ball_使用代码: Ball ball1; void setup(){ size(500,500); ball1 = new Ball(); } void draw(){ background(

我正在努力使单个球在整个屏幕上反弹,但我在使球反弹方面有问题bounce()函数负责使球反弹,但我遇到了数组越界异常,尽管只通过数组中的1个元素。我提供了下面的代码。此代码使用2个类(Ball\u用法和Ball)

Ball_使用代码:

Ball ball1;

    void setup(){
     size(500,500);
     
     ball1 = new Ball();
     
    }
    
    void draw(){
     background(255);
     ball1.bounce();
    }
球:

班级舞会{
float[]xPos={};
float[]yPos={};
浮动[]直径={};
float[]xSpeed={};
float[]ySpeed={};
Ball(){
}
球(浮子x、浮子y、浮子argDia){
xPos=append(xPos,x);
yPos=追加(yPos,y);
dia=附加(dia,argDia);
}
无效反弹(){

对于(int i=0;i我认为混淆来自于您的类有两个构造函数的事实:

  • 空构造函数(不带参数):
    Ball()
  • 具有位置和
    argDia
    (猜测直径?)参数的构造函数:
    Ball(float x,float y,float argDia)
setup()
中调用空构造函数:

ball1 = new Ball();
Ball ball1;

void setup() {
  size(500, 500);

  //ball1 = new Ball();
  ball1 = new Ball(250, 250, 50);
}

void draw() {
  background(255);
  ball1.bounce();
}

class Ball {
  float [] xPos = {};
  float [] yPos = {};
  float [] dia = {};
  float [] xSpeed = {};
  float [] ySpeed = {};

  Ball() {
  }

  Ball(float x, float y, float argDia) {
    xPos = append(xPos, x);
    yPos = append(yPos, y);
    dia = append(dia, argDia);
    xSpeed = append(xSpeed, random(-1, 1));
    ySpeed = append(ySpeed, random(-1, 1));
  }

  void bounce() {
    for (int i=0; i<1; i++) {
      ellipse(xPos[i], yPos[i], 50, 50);
      xPos[i] += xSpeed[i];
      yPos[i] += ySpeed[i];
      if (xPos[i]<0 || xPos[i]>=width) {
        xSpeed[i] = -xSpeed[i];
      }

      if (yPos[i]<0 || yPos[i]>=height) {
        ySpeed[i] = -ySpeed[i];
      }
    }
  }
}
这意味着五个浮点数组的
长度仍然为0,因此存在越界异常

即使调用构造函数的位置+直径版本,
xSpeed
ySpeed
数组仍将具有
长度
0

您可以通过初始化两个数组以及使用此版本的构造函数来解决此问题:

ball1 = new Ball();
Ball ball1;

void setup() {
  size(500, 500);

  //ball1 = new Ball();
  ball1 = new Ball(250, 250, 50);
}

void draw() {
  background(255);
  ball1.bounce();
}

class Ball {
  float [] xPos = {};
  float [] yPos = {};
  float [] dia = {};
  float [] xSpeed = {};
  float [] ySpeed = {};

  Ball() {
  }

  Ball(float x, float y, float argDia) {
    xPos = append(xPos, x);
    yPos = append(yPos, y);
    dia = append(dia, argDia);
    xSpeed = append(xSpeed, random(-1, 1));
    ySpeed = append(ySpeed, random(-1, 1));
  }

  void bounce() {
    for (int i=0; i<1; i++) {
      ellipse(xPos[i], yPos[i], 50, 50);
      xPos[i] += xSpeed[i];
      yPos[i] += ySpeed[i];
      if (xPos[i]<0 || xPos[i]>=width) {
        xSpeed[i] = -xSpeed[i];
      }

      if (yPos[i]<0 || yPos[i]>=height) {
        ySpeed[i] = -ySpeed[i];
      }
    }
  }
}
这看起来更类似于。 您可以在稍后的阶段创建
Ball
对象的数组


此外,格式化代码是值得的,因为它可以节省您阅读/滚动代码的时间,并且在视觉上更容易扫描程序的结构(每个部分如何适应)这样就更容易在头脑中调试/运行。在Windows/Linux上只需按
Ctrl+T
,或者在OSX上只需按
CMD+T
,这不费吹灰之力。从长远来看,这会带来回报,特别是当程序变得越来越长,越来越复杂,因为你花在阅读代码上的时间比写代码的时间多。在学习的时候尽早学习是一个好习惯开始编写代码吧。祝你玩得开心!

我相信混淆是因为你的类有两个构造函数:

  • 空构造函数(不带参数):
    Ball()
  • 具有位置和
    argDia
    (猜测直径?)参数的构造函数:
    Ball(float x,float y,float argDia)
setup()
中调用空构造函数:

ball1 = new Ball();
Ball ball1;

void setup() {
  size(500, 500);

  //ball1 = new Ball();
  ball1 = new Ball(250, 250, 50);
}

void draw() {
  background(255);
  ball1.bounce();
}

class Ball {
  float [] xPos = {};
  float [] yPos = {};
  float [] dia = {};
  float [] xSpeed = {};
  float [] ySpeed = {};

  Ball() {
  }

  Ball(float x, float y, float argDia) {
    xPos = append(xPos, x);
    yPos = append(yPos, y);
    dia = append(dia, argDia);
    xSpeed = append(xSpeed, random(-1, 1));
    ySpeed = append(ySpeed, random(-1, 1));
  }

  void bounce() {
    for (int i=0; i<1; i++) {
      ellipse(xPos[i], yPos[i], 50, 50);
      xPos[i] += xSpeed[i];
      yPos[i] += ySpeed[i];
      if (xPos[i]<0 || xPos[i]>=width) {
        xSpeed[i] = -xSpeed[i];
      }

      if (yPos[i]<0 || yPos[i]>=height) {
        ySpeed[i] = -ySpeed[i];
      }
    }
  }
}
这意味着五个浮点数组的
长度仍然为0,因此存在越界异常

即使调用构造函数的位置+直径版本,
xSpeed
ySpeed
数组仍将具有
长度
0

您可以通过初始化两个数组以及使用此版本的构造函数来解决此问题:

ball1 = new Ball();
Ball ball1;

void setup() {
  size(500, 500);

  //ball1 = new Ball();
  ball1 = new Ball(250, 250, 50);
}

void draw() {
  background(255);
  ball1.bounce();
}

class Ball {
  float [] xPos = {};
  float [] yPos = {};
  float [] dia = {};
  float [] xSpeed = {};
  float [] ySpeed = {};

  Ball() {
  }

  Ball(float x, float y, float argDia) {
    xPos = append(xPos, x);
    yPos = append(yPos, y);
    dia = append(dia, argDia);
    xSpeed = append(xSpeed, random(-1, 1));
    ySpeed = append(ySpeed, random(-1, 1));
  }

  void bounce() {
    for (int i=0; i<1; i++) {
      ellipse(xPos[i], yPos[i], 50, 50);
      xPos[i] += xSpeed[i];
      yPos[i] += ySpeed[i];
      if (xPos[i]<0 || xPos[i]>=width) {
        xSpeed[i] = -xSpeed[i];
      }

      if (yPos[i]<0 || yPos[i]>=height) {
        ySpeed[i] = -ySpeed[i];
      }
    }
  }
}
这看起来更类似于。 您可以在稍后的阶段创建
Ball
对象的数组

此外,格式化代码是值得的,因为它可以节省您阅读/滚动代码的时间,并且在视觉上更容易扫描程序的结构(每个部分如何适应)这样就更容易在头脑中调试/运行。在Windows/Linux上只需按
Ctrl+T
,或者在OSX上只需按
CMD+T
,这不费吹灰之力。从长远来看,这会带来回报,特别是当程序变得越来越长,越来越复杂,因为你花在阅读代码上的时间比写代码的时间多。在学习的时候尽早学习是一个好习惯宁到代码。玩得开心