You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Khan Academy项目:applyForce函数报错与Buglight发光异常求助

解决你的Khan Academy项目问题

嘿,我来帮你搞定这两个问题!咱们一个一个来拆解:

问题1:applyForce is not a function 报错

这个问题很直接——你的Mover类根本没定义applyForce方法,调用的时候自然会抛出错误。咱们需要给Mover原型添加这个方法,按照牛顿第二定律F=ma,把传入的力除以物体质量后,加到加速度上即可:

Mover.prototype.applyForce = function(force) {
  // 根据质量计算加速度(F=ma → a=F/m)
  var f = PVector.div(force, this.mass);
  this.acceleration.add(f);
};

问题2:Buglight的glow值变化过快,仅显示最大发光量

目前你的Buglight没有控制发光量渐变的逻辑,直接赋值当然会跳变。咱们可以给Buglight加一个glowSpeed属性来控制变化速率,然后在update方法里逐步调整glow的值,同时设置上下边界,达到边界时反转速度,实现平滑的呼吸效果:

修改Buglight的构造函数

给Buglight添加glowSpeed属性(数值可以根据需求调整,越小变化越慢):

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;
  this.glowSpeed = 1; // 新增:控制发光量变化速率
};

修改Buglight的update方法

在update里处理glow的渐变逻辑:

Buglight.prototype.update = function() {
  // 逐步调整发光量
  this.glow += this.glowSpeed;
  // 设置发光量的上下限(这里设为50-100,你可以自己修改范围)
  if (this.glow > 100 || this.glow < 50) {
    this.glowSpeed *= -1; // 达到边界时反转方向
  }
  // 绘制发光效果
  noStroke();
  fill(39, 131, 207,90);
  ellipse(this.position.x+20,this.position.y+35,this.glow,this.glow);
};

完整修改后的代码

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.applyForce = function(force) {
  var f = PVector.div(force, this.mass);
  this.acceleration.add(f);
};

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;
  this.glowSpeed = 1;
};

Buglight.prototype.update = function() {
  this.glow += this.glowSpeed;
  if (this.glow > 100 || this.glow < 50) {
    this.glowSpeed *= -1;
  }
  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();
  }
};

内容的提问来源于stack exchange,提问作者jj1993

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:24:09