gpt4 book ai didi

javascript - 构造函数在 p5 js 中无法正常工作

转载 作者:行者123 更新时间:2023-12-03 01:59:05 25 4
gpt4 key购买 nike

    我在使用 p5 js 时遇到问题。
我正在尝试添加两个绕圆圆周旋转的椭圆形对象。下面的代码代表了对象的构造函数:

function Obj(){
this.ang = TWO_PI;
this.x = w/2 + cos(ang)*r;
this.y = h/2 + sin(ang)*r;
this.fil = 255;
this.size = 28;
this.show = function(){
noStroke();
fill(this.fil);
ellipse(this.x,this.y,this.size,this.size);
}
this.update = function(){
this.ang+=.02;
}
}


这是主文件:

let w = innerWidth;
let h = innerHeight;
let xd = [];
let r = 200;
function setup() {
createCanvas(w, h);
for (let i = 0; i < 2; i++)
xd[i] = new Obj();
}
function draw(){
background(0,70,80);
noFill();
strokeWeight(7);
stroke(255);
ellipse(w/2, h/2, r*2, r*2);
xd[0].update();
xd[0].show();
}

问题是它说ang is not Defined,即使我确实用this.ang = TWO_PI;明确定义了它。如果我在主文件和 setup() 中声明它,我说 ang = TWO_PI; 对象保持在原位。有人可以帮忙吗?

谢谢。

最佳答案

这段代码中的构造函数有问题,应该是这样的:

this.x = w/2 + cos(this.ang)*r;
this.y = h/2 + sin(this.ang)*r;

因为您正在使用构造函数本身的属性

关于javascript - 构造函数在 p5 js 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50121546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com