processing代码出错

#这个代码加了定义之后void setup()还有void draw()都无法运行,就连最简单的size()都运行不起来但是把定义删了size();就能运行了。 会提示语法错误。但我感觉我定义也是对的,为什么运行不起来.

他会提示Missing operator, semicolon, or ‘}’ near ‘setup’?


ArrayList balls;
PImage=img;
float s=1.8;
import processing.sound.*;
AudioIn input;
Amplitude loundness;
void setup(){
  fullScreen (P2D) ;
  smooth();
  noStroke();
  balls = new ArrayList  () ;
  img = loadImage ("2.jpg") ;
  img.resize((int) (img. width*s), (int) (img. height*s));
  
  input = new AudioIn(this, 0) ;
  input. start ();
  loudness = new Amplitude (this) ;
  loudness.input (input) ;
}

void draw() {
  float inputLevel = 0.5;//map (mouseY, 0, height, 1.0, 0. 0);
  input. amp (inputLevel);
  float volume  = loudness. analyze ();
  for (int i = 30; i<map (volume, 0, 0.5, 1, 200); i+=2) {
    int x = int (random (img.width));
    int y = int (random (img.height));
    color pix = img.get (x, y);
    int a = (int) random (4);
    if (a == 2)
      balls. add (new Ball(0, height/2, x, y, pix)) ;
    else if (a == 1)
      balls. add (new Ball(width, height/2, x, y, pix));
    else if (a == 2)
      balls. add (new Ball(width/2, 0, x, y, pix));
     else
     balls. add (new Ball(width/2, height, x, y, pix));
  }
  background(255);
  for (int i=0; i<=balls.size()-1; i++) {
    Ball ball=balls.get(i);
    ball.move();
    ball.display();
    if (ball.finishied()) {
      balls.remove(i);
    }
  }
}

这是小球的类程序


class Ball {
  float x;
  float y;
  float size=random(5, 15);
  float life=255;
  color c;
  float x0=0;
  float y0=height/2;
  Ball(float _x0, float _y0, float tempX, float tempY, color _c) {
    x=tempX+width/2-img.width/2;
    y=tempY=height/2-img.heigh/2;
    c=_c;
    x0=_x0;
    y0=_y0;
  }

  void move() {
    x0+=(x-x0)/50.0;
    y0+=(y-y0)/50.0;
  }
  boolean finished() {
    if (!cheak)
      life--;
    if (life<0) {
      return true;
    } else {
      return false;
    }
  }

  void display() {
    fill(c, life);
    ellipse(x0, y0, size, size);
  }
}

我发现了几个错误,请您检查一下:

1.PImage=img; 应该写成 PImage img;
2.fullScreen (P2D) ; 中应该是 fullScreen (P2D);
3.img.resize((int) (img.widths), (int) (img.heights)); 中应该是img.width和img.height而不是img.width和img.heigh。
4.在 Ball 类中的 y=tempY=height/2-img.heigh/2; 应该是 y=tempY+height/2-img.height/2;
5.在 Ball 类中的 if (!cheak) 应该是 if (!finished())
修改以上错误后应该可以解决这个问题。如果还有其他错误或者不清楚的地方,请再次提问。

提示setup附近缺少分号或者结束符号},检查是否有缺少分号的语法错误,以及核对一下这个大括号{和这个结束的大括号}是否是成双成对的,是否有缺少或多余的。

可能是因为在void setup()和void draw()函数中使用了不正确的语法。
我大概看了一下

fullScreen (P2D) ; 应该是 fullScreen (P2D);
PImage=img; 应该是 PImage img;
img.resize((int) (img. widths), (int) (img. heights)); 中的img. width应该是img.width,img. height应该是img.height
float volume = loudness. analyze (); 的 volume 应该是 volume
for (int i = 30; i<map (volume, 0, 0.5, 1, 200); i+=2) 中的i<map应该是i<=map
int a = (int) random (4); 这里a是用来判断小球在哪里出现的,但是if (a == 2) 和else if (a == 2) 里面都是a==2,应该改为if (a == 1) 和else if (a == 3)
if (ball.finishied()) 应该是if (ball.finished())
Ball(float _x0, float _y0, float tempX, float tempY, color _c) 中的y=tempY=height/2-img.heigh/2应该是y=tempY+height/2-img.height/2
boolean finished() 中的if (!cheak)应该是if (!check)

代码中存在一些错误,比如:

  1. y=tempY=height/2-img.heigh/2; 应该是y=tempY+height/2-img.height/2;
  2. cheak 应该是 check
  3. PImage=img; 应该是 PImage img;

这个问题可能是因为在定义变量时有语法错误,导致后面的代码无法正常运行。

如果你在定义变量时忘记了使用分号,就会导致编译器出现错误。

例如:

int a = 0

这样的话编译器会提示 Missing operator, semicolon, or ‘}’ near ‘setup’

正确的写法应该是

int a = 0

所以我建议你检查一下你的定义是否有语法错误。

另外,如果你的代码中包含其他的错误,它们可能会导致后面的代码无法正常运行。因此,请检查您的代码中是否存在其他错误。