繁体   English   中英

仅当类文件在名称中包含括号时才运行JavaScript

[英]JavaScript only running when class file contains parenthesis in name

我是JavaScript的新手,想要编写一个我以前用Java编写过的游戏。 我正在使用p5.js库进行编码,并使用Atom文本编辑器。 我正在制作的游戏是小行星,它运行良好。 我已经想出了如何添加船和激光器。 然后我添加了实际小行星的代码。 (我不确定这是否相关,但正如我之前所说,我正在从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);
  }
}

运行代码,我得到了同样的错误。 结果是你的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); } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM