vehicle.stop不是函数p5.js

问题描述 投票:0回答:1

我开始学习p5.js,为了帮助我学习,我已经开始创建“驾驶游戏”。甚至还没有完成,但是我遇到了一个错误,说“ vehicle.stop不是函数”。游戏的重点是让用户按下向上箭头来“驾驶”汽车,即使在按下汽车时不会发生任何运动。用户总共可以玩60秒,但是在游戏中的某些时候会出现一个停车标志,然后用户必须尽快击打空格键。如果在出现符号的1秒钟内按下空格键,则用户将获得该点。我不确定为什么会收到错误消息,我想知道是否有人可以向我指出正确的方向。任何帮助表示赞赏! :)

var count = 6;
let timer = 60;
var score = 0;

function preload() {

  car = loadImage('car.png');
  stop = loadImage('stop.png');

}

function setup() {
  createCanvas(400, 400);

  background(100, 200, 255);

  stroke(150);
  strokeWeight(120);
  line(1000, -2000, 10, 400);

  line(600, 1000, 250, 20);

  strokeWeight(250);
  line(200, 600, 200, 200);

  strokeWeight(5);
  stroke(255);
  line(210, 100, 210, 10);

  line(210, 300, 210, 150);

  image(car, 110, 220);


 }

function draw() {

  strokeWeight(1);
  stroke(200);
  fill(0);
  text("Score: " + score, 20, 30);
  text("Time Left: " + timer, 20, 45);

  if ((frameCount % 120 == 0) && (timer > 0)) {
  timer--;
  }

  if ((timer == 60) || (score == 0)) {
  fill(0);
  rect(115, 225, 200, 70);
  fill(255);
  text("START DRIVING GAME ", 142, 250);
  text("CLICK BUTTON TO BEGIN", 135, 265);
  text("USE UP-ARROW KEY TO DRIVE", 130, 280);
  }

  if ((timer == 0) || (score == count)) {
  fill(0);
  rect(130, 220, 125, 70);
  fill(255);
  text("GAME OVER", 150, 250);
  text("You scored: " + score, 150, 265);
  }


  }

  function mouseClicked() {
    vehicle.stop();

  }

  function keyPressed() {
    if (keyCode == UP_ARROW) {
      car.go();
  } else if (keyCode == 32) {
  car.stop();
  }
  }

  function vehicle() {

  this.go = function() {
  goTime = new Date();

  }

  this.stop = function() {

  if ((timer == 55) || (timer == 40) || (timer == 32) || (timer == 23) || (timer == 15) ||             
  (timer == 6)) {

  image(stop, 170, 90);

  }
  stopTime = new Date();
  if ((vehicle.stop) && (elapsedTime == 1)) {
  score = score + 1;
  } else(score = score + 0);

  }

  elapsedTime = goTime - stopTime;

  }
javascript p5.js
1个回答
0
投票

嘿:) Rabbid76指出代码中还有其他错误。

未找到vehicle.stop的原因是,需要使用'new'关键字来定义“构造函数”(如您编写的车辆函数)。

let myVehicle = new vehicle();

myVehicle.stop() //should work, once you fix the additional errors.

此页面上有一些很好的信息:https://www.w3schools.com/js/js_object_constructors.asp

我总是喜欢使用类。https://www.w3schools.com/js/js_classes.asp

© www.soinside.com 2019 - 2024. All rights reserved.