【问题标题】:unexpected EOF error in JSJS中的意外EOF错误
【发布时间】:2018-08-06 08:48:18
【问题描述】:

我正在为遗传算法编写这段代码,但是当我运行这段代码时,我在绘制函数之后不断收到意外的 EOF 错误。

SyntaxError: 预期 eof 但找到 }

我不太擅长 javascript,所以我不知道我是否犯了愚蠢的错误或什么。在我改变某些东西之前它似乎工作正常?我不知道,但它不再起作用了,也许我只是没有看到它......

var population;
var lifespan = 200;
var lifeP;
var count = 0;

function setup() {
  createCanvas(400,300);
  rocket = new Rocket();
  population = new Population();
  lifeP = createP();
}

function draw() {
  background(0); 
  population.run();
  lifeP.html(count);
  count++;
}

function Population() {
 this.rockets = [];
 this.popsize = 25;


 for(var i = 0; i < this.popsize; i++){
  this.rockets[i] = new Rocket(); 
 }

 this.run = function() {
   for(var i = 0; i < this.popsize; i++){
    this.rockets[i].update();
    this.rockets[i].show();
   }
 }
}


function DNA(){
 this.genes = [];
 for (var i = 0; i < lifespan; i++){
   this.genes[i] = p5.Vector.random2D();
   this.genes[i].setMag(0.1);
 }
}

function Rocket(){
 this.pos = createVector(width/2, height);
 this.vel = createVector();
 this.acc = createVector();
 this.dna = new DNA();;
 this.applyForce = function(force){
  this.acc.add(force);   
 }

 this.update = function(){
  this.applyForce(this.dna.genes[count]);

  this.vel.add(this.acc);
  this.pos.add(this.vel);
  this.acc.mult(0);
 }

 this.show = function(){
  push();
  noStroke();
  fill(255, 150);
  translate(this.pos.x, this.pos.y); 
  rotate(this.vel.heading());
  rectMode(CENTER);
  rect(0, 0, 25, 5);
  pop();
 }


}

【问题讨论】:

    标签: javascript syntax-error genetic-algorithm eof


    【解决方案1】:

    你的火箭类应该是这样的:

    function Rocket(){
     this.pos = createVector(width/2, height);
     this.vel = createVector();
     this.acc = createVector();
     this.dna = new DNA();
     this.applyForce = function(force){
      this.acc.add(force);   
     }
    
     this.update = function(){
      this.applyForce(this.dna.genes[count]);
    
      this.vel.add(this.acc);
      this.pos.add(this.vel);
      this.acc.mult(0);
     }
    
     this.show = function(){
      push();
      noStroke();
      fill(255, 150);
      translate(this.pos.x, this.pos.y); 
      rotate(this.vel.heading());
      rectMode(CENTER);
      rect(0, 0, 25, 5);
      pop();
     }
    }

    但是下次,请在编辑器中检查您的代码,例如 jsfiddle 不要向stackoverflow发送垃圾邮件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 2013-07-13
      相关资源
      最近更新 更多