【问题标题】:processing.js unexpected identifier at if()if() 处的 processing.js 意外标识符
【发布时间】:2016-11-02 08:37:50
【问题描述】:

我似乎找不到错误,但是当我删除 if 语句时,它运行良好。代码很短,应该很容易找到问题。

int zw1;
int zw2;
int px = 100;
int py = 100;
int ppx = px + random(-20, 20);
int ppy = py + random(-20, 20);
int cx;
int cy;
int xrand = 50;
int yrand = 50;
int opacity = 49;
frameRate(30);
background(74, 71, 74);
int timetoclear = 0;

int x0 = 0;
int y0 = 0;
void setup() {
    size(400, 400);

}
void draw() {

    cx = px + random(-xrand, xrand);
    cy = py + random(-xrand, xrand);

    // fill(74, 72, 74,5);
    // rect(-10,-10,1000,1000);

    //cx = mouseX;
    //cy = mouseY;  
    if (cx <= 0) {
        cx = 0;
    }
    if (cx >= 400) {
        cx = 400;
    }
    if (cy <= 0) {
        cy = 0;
    }
    if (cy >= 640) {
        cy = 640;
    }

    stroke(30 + random(-100, 100), 195 + random(-100, 100), 201, 60);

    fill(30 + random(-100, 100), 195 + random(-100, 100), 201, 50);

    triangle(ppx, ppy, px, py, cx, cy);

    ppx = px;
    ppy = py;
    px = cx;
    py = cy;
}

抛出: Uncaught SyntaxError: Unexpected Identifier

【问题讨论】:

  • 请提供准确的错误信息。
  • Uncaught SyntaxError: Unexpected Identifier
  • JavaScript 怎么样?
  • 这不是完整的错误,它也会给你一个行号和/或列。
  • 不应该frameratebackgroundsetup 函数中吗?

标签: javascript identifier processing.js


【解决方案1】:

如果我在这里运行你的代码对我来说很好:http://processingjs.org/tools/processing-helper.html

您的代码到底是如何运行的?

我强烈建议在 Java 模式下编程,然后切换到 JavaScript 模式仅用于导出。这将捕获您遇到的所有小错误,例如:

  • 在调用setup() 函数之前,您不应调用frameRate()background()random()。在 setup() 函数中移动它们。

  • random() 函数返回 float 值,但您将其存储在 int 变量中。 JavaScript 不会抱怨,但会导致奇怪的行为。

【讨论】:

    【解决方案2】:

    不知道为什么会失败,但您可以尝试以下解决方法:

    cx = Math.max(0, Math.min(400, cx));
    cy = Math.max(0, Math.min(640, cy));
    

    而不是

    if (cx <= 0) {
        cx = 0;
    }
    if (cx >= 400) {
        cx = 400;
    }
    if (cy <= 0) {
        cy = 0;
    }
    if (cy >= 640) {
        cy = 640;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-14
      • 2017-09-05
      • 2020-07-10
      • 2012-06-19
      • 2014-04-10
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多