我正在研究遗传算法的这段代码,但是当我运行此代码时,我会在绘制函数后立即收到意外的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();
}
}
答案 0 :(得分:0)
你的火箭课应该是这样的:
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();
}
}
&#13;
但是下一次,请在编辑器中检查您的代码 jsfiddle并且不会发送垃圾邮件堆栈溢出