Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Processing 是否将对象与此代码一起使用?处理_Processing - Fatal编程技术网

Processing 是否将对象与此代码一起使用?处理

Processing 是否将对象与此代码一起使用?处理,processing,Processing,如何将我现在的代码用于一个可以存储球反弹次数、颜色(添加随机颜色时)和速度的对象。任何指点或提示都将非常有用。我是OOP新手,它会让我感到困惑。提前谢谢 float x; float y; float yspeed = 0; float xspeed = 0; float balldiameter = 10; float ballradius = balldiameter/2; void setup() { size (400,400); backgroun

如何将我现在的代码用于一个可以存储球反弹次数、颜色(添加随机颜色时)和速度的对象。任何指点或提示都将非常有用。我是OOP新手,它会让我感到困惑。提前谢谢

  float x;
  float y;
  float yspeed = 0;
  float xspeed = 0;
  float balldiameter = 10;
  float ballradius = balldiameter/2;

  void setup() {
  size (400,400);
  background (255);
  fill (0);
  ellipseMode(CENTER);
  smooth();
  noStroke();
  x = random(400);
  y = 0;
  }

  void draw() {
  mouseChecks();
  boundaryChecks();
  ballFunctions();
  keyFunctions();
  }

  void mouseChecks() {
    if (mousePressed == true) {
    x = mouseX;
    y = mouseY;
    yspeed = mouseY - pmouseY;
    xspeed = mouseX - pmouseX;
    }
  }

  void boundaryChecks() {
    if (y >= height - ballradius) {
      y = height - ballradius;
      yspeed = -yspeed/1.15;
    }
    if (y <= ballradius) {
      y = ballradius;
      yspeed = -yspeed/1.35;
    }
    if (x >= width -ballradius) {
      x = width -ballradius;
      xspeed = -xspeed/1.10;
    }
    if (x <= ballradius) {
      x = ballradius;
      xspeed = -xspeed/1.10;
     }
   }

   void ballFunctions() {
   if (balldiameter < 2) {
     balldiameter = 2;
     }
   if (balldiameter > 400) {
     balldiameter = 400;
     }
   ballradius = balldiameter/2;
   background(255); //should this be in here?
   ellipse (x,y,balldiameter,balldiameter);
   yspeed = yspeed += 1.63;
    // xspeed = xspeed+=1.63;
   y = y + yspeed;
   x = x + xspeed; 
   }
  void keyFunctions() {
    if (keyPressed) {
      if(keyCode == UP) {
      balldiameter +=1;
    }
    if (keyCode == DOWN) {
      balldiameter -=1;
      }
    }
   }
float x;
浮动y;
浮动Y速度=0;
float xspeed=0;
浮球直径=10;
浮球半径=球直径/2;
无效设置(){
尺寸(400400);
背景(255);
填充(0);
ellipseMode(中心);
光滑的();
仰泳();
x=随机(400);
y=0;
}
作废提款(){
mouseChecks();
边界检查();
ball函数();
键函数();
}
void mouseChecks(){
if(mousePressed==true){
x=鼠标;
y=老鼠;
yspeed=mouseY-pmouseY;
xspeed=mouseX-pmouseX;
}
}
无效边界检查(){
如果(y>=高度-球半径){
y=高度-球半径;
yspeed=-yspeed/1.15;
}
如果(y=宽度-球半径){
x=宽度-球半径;
xspeed=-xspeed/1.10;
}
如果(x 400){
球径=400;
}
球半径=球直径/2;
背景(255);//应该在这里吗?
椭圆(x,y,球径,球径);
yspeed=yspeed+=1.63;
//xspeed=xspeed+=1.63;
y=y+y速度;
x=x+x速度;
}
void键函数(){
如果(按键){
if(keyCode==UP){
球径+=1;
}
如果(键代码==向下){
球径-=1;
}
}
}

您可能需要执行以下操作:
创建名为
Ball.pde的新文件
在该文件中写入:

public class Ball {
    public float x;
    public float y;
    public float yspeed;
    public float xspeed;
    public float diameter;
    public float radius;  

    public Ball(float initial_x, float initial_y, float diam) {
        this.x = initial_x;
        this.y = initial_y;
        this.xspeed = 0;
        this.yspeed = 0;
        this.diameter = diam;
        this.radius = diam/2;
    }

    public void move() {
       // movement stuff here
    }
}
这将为您提供一个非常基本的
Ball
类。现在可以在主草图文件中使用此类,如下所示:

Ball my_ball = new Ball(50, 50, 10);
您可以使用以下方式访问balls成员:

my_ball.xspeed;
my_ball.yspeed;
my_ball.anything_you_defined_in_ball;
这将允许您将球的所有相关变量存储在它自己的类中。您甚至可以创建多个

Ball my_ball1 = new Ball(50, 50, 10);
Ball my_ball2 = new Ball(20, 20, 5);

需要注意的是,在处理过程中,不需要为此创建新文件,代码可以放在同一个文件中(下面指出了非常糟糕的做法),也可以放在IDE的新选项卡中。如果您使用的是处理IDE,您可以从右侧的箭头菜单中选择“新建选项卡”,它将为您创建文件。它将具有“.pde”扩展名。

我认为您需要将所有内容封装在一个函数中,并决定要将对象的哪些部分设置为变量。为每个部分创建一个参数作为变量。这应该是一个注释。将所有类放在同一个文件中是非常糟糕的做法。IDE中的新选项卡是一个新文件。我当时无法评论。。。我说一个人不需要,不应该。因为在处理过程中,人们通常对编程非常陌生(就像我一样),我认为有时候简单可能更重要。无论如何,谢谢你指出这一点。我一直在学习。我应该复制并粘贴它作为评论吗?啊,我明白了,我不喜欢那个不允许评论的规则。。。你的答案很好:)为什么是Ball.ps?不应该是Ball.pde吗?还是Ball.java?