javascript 无法将“v1.copy”作为函数调用

pkwftd7m  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(119)

我一直在遵循丹尼尔Shiffman的一个关于转向行为(寻找)的教程,基于该教程,我创建了一个包含两个对象(车辆,目标)的类,然后将它们添加到主草图中;现在我得到了这个错误,它指向了一个甚至不存在的奇怪的行。
p5js web editor error
我用vs code和chrome测试了同样的代码,chrome的控制台仍然显示了同样的错误,只是引用了另一个奇怪的行。
该代码的主要目的是飞行器必须寻找目标。
这是主要的草图:

let vehicle;
let target;

function setup() {
  createCanvas(400, 400);
  vehicle = new Vehicle(50, 50);
  target = new Target(mouseX, mouseY);
}

function draw() {
  background(220);

  let steering = vehicle.seek(target);
  vehicle.applyForce(steering);

  vehicle.show();
  vehicle.update();
  
  target.show();
  target.update();
}

这是交通工具类

class Vehicle {
  constructor(x, y) {
    this.pos = createVector(x, y);
    this.vel = createVector(0, 0);
    this.acc = createVector(0, 0);
    this.maxSpeed = 4;
    this.maxForce = 1;
    this.r = 16;
  }

  seek(target) {
    let force = p5.Vector.sub(target, this.pos);
    force.setMag(this.maxSpeed);
    force.sub(this.vel);
    force.limit(this.maxForce);
    return force;
  }

  applyForce(force) {
    this.acc.add(force);
  }

  update() {
    this.vel.add(this.acc);
    this.vel.limit(this.maxSpeed);
    this.pos.add(this.vel);
    this.acc.set(0, 0);
  }
  show() {
    noStroke();
    fill(0);
    push();
    translate(this.pos.x, this.pos.y);
    rotate(this.vel.heading());
    triangle(-this.r, -this.r / 2, -this.r, this.r / 2, this.r, 0);
    pop();
  }
}

class Target extends Vehicle {
  constructor(x, y) {
    super(x, y);
  }
  show() {
    noStroke();
    fill(200, 0, 0);
    ellipse(this.pos.x, this.pox.y, this.r);
  }
}
mrfwxfqh

mrfwxfqh1#

你不会喜欢它的,但它是错别字:)

  • this.pos.y,而不是第71行上的this.pox.y
  • let force = p5.Vector.sub(target.pos, this.pos);(不是第36行上的let force = p5.Vector.sub(target, this.pos);

您的代码在其他方面运行良好:

let vehicle;
let target;

function setup() {
  createCanvas(400, 400);
  vehicle = new Vehicle(50, 50);
  target = new Target(mouseX, mouseY);
}

function draw() {
  background(220);
  
  target.pos.set(mouseX, mouseY);
  vehicle.applyForce(vehicle.seek(target));

  vehicle.show();
  vehicle.update();
  
  target.show();
  target.update();
}

class Vehicle {
  constructor(x, y) {
    this.pos = createVector(x, y);
    this.vel = createVector(0, 0);
    this.acc = createVector(0, 0);
    this.maxSpeed = 4;
    this.maxForce = 1;
    this.r = 16;
  }

  seek(target) {
    let force = p5.Vector.sub(target.pos, this.pos);
    force.setMag(this.maxSpeed);
    force.sub(this.vel);
    force.limit(this.maxForce);
    return force;
  }

  applyForce(force) {
    this.acc.add(force);
  }

  update() {
    this.vel.add(this.acc);
    this.vel.limit(this.maxSpeed);
    this.pos.add(this.vel);
    this.acc.set(0, 0);
  }
  show() {
    noStroke();
    fill(0);
    push();
    translate(this.pos.x, this.pos.y);
    rotate(this.vel.heading());
    triangle(-this.r, -this.r / 2, -this.r, this.r / 2, this.r, 0);
    pop();
  }
}

class Target extends Vehicle {
  constructor(x, y) {
    super(x, y);
  }
  show() {
    noStroke();
    fill(200, 0, 0);
    ellipse(this.pos.x, this.pos.y, this.r);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.5.0/p5.min.js"></script>

相关问题