这是来自一个Khanacademy项目 - 我想知道为什么当我尝试将我的暗光中的排斥力应用到每个苍蝇时为什么它会返回__env__.flies[j].applyForce is not a function
。
我一直在搞乱这个并且无法接受这种力量。此外,当我尝试通过添加一个数量然后移除金额来改变该功能中的错误时,它看起来发生得太快,因为它只显示最大发光量。
CODE:
//我在鼠标所在的位置创建了一个灯光,并且苍蝇(随机开始的poisitions)被绘制成了光线,但是我为它们的动作添加了一些随机性,以便为它们的路径添加一些变化,苍蝇被排除在使用排斥力的buglight:
var Mover = function() {
this.position = new PVector (random(width),random(height));
this.velocity = new PVector (0,0);
this.acceleration = new PVector (0,0);
this.mass = 1;
};
Mover.prototype.update = function () {
var mouse = new PVector(mouseX,mouseY);
var dist = PVector.sub(mouse, this.position);
var randomPush = new PVector(random(-1,1),random(-1,1));
dist.normalize();
dist.mult(0.4);
this.acceleration = dist;
this.acceleration.add(randomPush);
this.velocity.add(this.acceleration);
this.velocity.limit(9);
this.position.add(this.velocity);
};
Mover.prototype.display = function () {
strokeWeight(7);
stroke(0, 0, 0);
point(this.position.x,this.position.y);
strokeWeight(5);
point(this.position.x - 3,this.position.y -3);
point(this.position.x + 3,this.position.y -3);
};
var Buglight = function (){
this.position = new PVector (random(width-50),random(height-80));
this.velocity = new PVector (0,0);
this.acceleration = new PVector (0,0);
this.glow = 70;
this.mass = 20;
this.G = 1;
};
Buglight.prototype.update = function() {
noStroke();
fill(39, 131, 207,90);
ellipse(this.position.x+20,this.position.y+35,this.glow,this.glow);
};
Buglight.prototype.repulsion = function(fly) {
var force = PVector.sub(this.position,fly.position);
var distance = force.mag();
distance = constrain(distance, 4.0, 20.0);
force.normalize();
var strength = -1 *(this.G * this.mass * fly.mass) / (distance * distance);
force.mult(strength);
return force;
};
Buglight.prototype.display = function() {
noStroke();
fill(5, 170, 252);
rect(this.position.x+15,this.position.y,10,60);
fill(0, 0, 0);
noStroke();
rect(this.position.x-10,this.position.y+60,60,10,10);
rect(this.position.x,this.position.y,10,60);
rect(this.position.x+30,this.position.y,10,60 );
quad(this.position.x,this.position.y,
this.position.x-10,this.position.y+20,
this.position.x+50,this.position.y+20,
this.position.x+40,this.position.y);
};
Buglight.prototype.checkEdges = function () {
};
var flies = [];
for (var i = 0; i < 4; i++){
flies[i] = new Mover();
}
var buglight = new Buglight();
var fly = new Mover();
draw = function() {
background(71, 71, 71);
strokeWeight(56);
stroke(219, 247, 8,100);
fill(255, 238, 0);
ellipse(mouseX,mouseY,20,20);
buglight.update();
buglight.display();
for (var j = 0; j < flies.length; j++) {
var blueForce = buglight.repulsion(flies[j]);
flies[j].applyForce(blueForce);
flies[j].update();
flies[j].display();
}
};
答案 0 :(得分:0)
您的Mover类上没有applyForce函数。我觉得有些东西不见了。