为什么这段代码不起作用。我在库 p5 中使用 javascript

Why doesn't this code work. I'm using javascript with the library p5

所以我一直在观看编码培训以学习 javascript 中的编码,我试图复制他的代码,但没有成功。我正在使用括号。这是剧集:

https://www.youtube.com/watch?v=fBqaA7zRO58&t=0s&list=PLRqwX-V7Uu6Zy51Q-x9tMWIv9cueOFTFA&index=26

let bubbles = []

function setup() {
    createCanvas(600,400) 
    for (let i = 0; i < 3; i++) {
        let x = 10 + 30 * i
        bubbles[i] = new Bubble(x, 200, 40);
    }
}

function draw() {
    background(0);
    for(let i = 0; i < bubbles.lenght; i++) {
        bubbles[i].move;
        bubbles[i].show;
    }
}

class Bubble {
    constructor(x,y,r) {
        this.x=x;
        this.y=y;
        this.r=r;
    }
    move() {
        this.x = this.x + random(-5,5);
        this.y = this.y + random(-5,5);
    }
    show() {
        stroke(255);
        strokeWeight(4);
        noFill();
        ellipse(this.x, this.y, this.r * 2);
    }
}        

请养成检查developer tools and debugging代码的习惯。

例如,我将从打印出您正在使用的变量的值开始。像这样:

console.log(bubbles.lenght);
for(let i = 0; i < bubbles.lenght; i++) {

你会看到这会打印出 undefined,这会给你一个提示:你拼写 length 错了!

解决该问题后,您还有其他问题。采取这些行:

bubbles[i].move;
bubbles[i].show;

这不是调用函数的方式。函数名称后需要 () 个括号:

bubbles[i].move();
bubbles[i].show();

退一步说,您真的不应该尝试像这样复制代码。相反,尝试从一个简单的示例开始,然后 work forward in small steps。然后如果你遇到错误,你会确切地知道是哪部分代码有问题。