仅当类文件名称中包含括号

时间:2019-04-05 12:38:43

标签: javascript google-chrome

我是JavaScript新手,想编写一个以前用Java编写过的游戏。我使用p5.js库进行编码,并使用Atom文本编辑器。我正在制作的游戏是Asteroids,并且运行良好。我已经弄清楚了如何添加飞船和激光器。然后,我添加了实际小行星的代码。 (我不确定这是否相关,但是正如我之前说过的,我是从Java项目中复制代码,当我添加小行星时,我基本上复制了整个内容,直到保存完为止。添加完之后,我尝试运行它,并说“未定义小行星”,因为我忘记了在HTML中添加对它的引用。但是,在我再次运行它之后,该页面停止加载并最终崩溃(显示“ Aw,Snap!显示此网页时出了点问题”错误)。我最初以为是HTML,但是当我将类添加到主脚本时,它仍然崩溃。一个小时后,我意识到,如果在类名(class ship(){})上加上括号,则会加载该页面。但是,我会得到预期的语法错误。如果像在Java中那样将对象声明为类(Ship ship;),将会发生相同的事情,它将运行,但是会显示“ Uncaught SyntaxError:Unexpected identifier”。我不确定如何解决这个问题,或者为什么不将括号添加到类名会导致程序无法运行。

附件是我的主要脚本的第一部分,我的飞船类的构造函数和我的小行星类,因为问题只有在添加它之后才开始。

任何帮助或解决此问题的方法将不胜感激。

主脚本:

let ship;
let bullets = [];
let asteroids = [];
//             Left   Right  Boost  Slow   Shoot
let keyDown = [false, false, false, false, false];
function setup(){
  window.canvas = createCanvas(800, 600);
  ship = new Ship(width/2, height/2);
  for(let i = 0; i = 4; i++){
    asteroids.push(new asteroid(random(width), random(height), 100));
  }
  frameRate(60);

}

function draw(){
  background(0);
  checkKeys();
  ship.update();
  ship.show();

  for(let i = bullets.length-1; i >= 0; i--){
    bullets[i].update();
    bullets[i].show();

    if(!bullets[i].alive){
      bullets.splice(i, 1);
    }
  }

  for(let i = asteroids.length-1; i >= 0; i--){
    asteroids[i].update();
    asteroids[i].show();
  }

}

船级:

class Ship{
  constructor(x, y){
    this.x = x;
    this.y = y;
    this.velX = 0;
    this.velY = 0;
    this.midX = this.x + 15;
    this.midY = this.y + 20;
    this.heading = 0;
    this.rotation = 0;
    this.boosting = false;
  }

小行星类:

class asteroid{
  constructor(x, y, size){
    this.x = x;
    this.y = y;
    this.size = size;
    this.velX = random(-2, 3);
    this.velY = random(-2, 3);

    if(this.velX == 0){
      this.velX++;
    }

    if(this.velY == 0){
      this.velY++;
    }

    this.points = random(4, 12);
    this.xPoints = [];
    this.yPoints = [];
    this.offsets = [];

    for(let i = 0; i < this.points; i++){
      this.offsets[i] = random(-size/5, (size/5)+1);
    }
  }

  update(){
    this.x += this.velX;
    this.y += this.velY;
  }

  show(){
    stroke(255);

    beginShape();
    for(let i; i < this.points; i++){
      let angle = i*(360/this.points);
      let px = (this.size/2 + this.offsets[i]) * cos(angle);
      let py = (this.size/2 + this.offsets[i]) * sin(angle);
      this.xPoints[i] = px + this.x;
      this.yPoints[i] = py + this.y;
      vertex(px, py);
    }
    endShape(CLOSE);
  }
}

1 个答案:

答案 0 :(得分:0)

运行代码,我遇到了同样的错误。原来您的ship类没有该类的闭括号},仅对于构造函数而言。添加,现在我没有错误

let ship;
let bullets = [];
let asteroids = [];
//             Left   Right  Boost  Slow   Shoot
let keyDown = [false, false, false, false, false];
function setup(){
  window.canvas = createCanvas(800, 600);
  ship = new Ship(width/2, height/2);
  for(let i = 0; i = 4; i++){
    asteroids.push(new asteroid(random(width), random(height), 100));
  }
  frameRate(60);

}

function draw(){
  background(0);
  checkKeys();
  ship.update();
  ship.show();

  for(let i = bullets.length-1; i >= 0; i--){
    bullets[i].update();
    bullets[i].show();

    if(!bullets[i].alive){
      bullets.splice(i, 1);
    }
  }

  for(let i = asteroids.length-1; i >= 0; i--){
    asteroids[i].update();
    asteroids[i].show();
  }

}


class Ship{
  constructor(x, y){
    this.x = x;
    this.y = y;
    this.velX = 0;
    this.velY = 0;
    this.midX = this.x + 15;
    this.midY = this.y + 20;
    this.heading = 0;
    this.rotation = 0;
    this.boosting = false;
  }
}  //<--- I added this bracket


class asteroid{
  constructor(x, y, size){
    this.x = x;
    this.y = y;
    this.size = size;
    this.velX = random(-2, 3);
    this.velY = random(-2, 3);

    if(this.velX == 0){
      this.velX++;
    }

    if(this.velY == 0){
      this.velY++;
    }

    this.points = random(4, 12);
    this.xPoints = [];
    this.yPoints = [];
    this.offsets = [];

    for(let i = 0; i < this.points; i++){
      this.offsets[i] = random(-size/5, (size/5)+1);
    }
  }

  update(){
    this.x += this.velX;
    this.y += this.velY;
  }

  show(){
    stroke(255);

    beginShape();
    for(let i; i < this.points; i++){
      let angle = i*(360/this.points);
      let px = (this.size/2 + this.offsets[i]) * cos(angle);
      let py = (this.size/2 + this.offsets[i]) * sin(angle);
      this.xPoints[i] = px + this.x;
      this.yPoints[i] = py + this.y;
      vertex(px, py);
    }
    endShape(CLOSE);
  }
}

相关问题