簡體   English   中英

我正在嘗試將 object 添加到 object 中的數組末尾

[英]I am trying to add an object to the end of an array inside said object

我有一些代碼可以創建一堆生物(細胞),它們在屏幕上移動並吃東西。 他們都有一個年齡變量,所以一旦他們達到一定年齡,他們就會嘗試克隆自己,但我不完全了解這應該如何工作或如何解決這個問題 go 我一直無法找到關於它在線。

它看起來像這樣 -->

let cells = [];
class Cell {
reproduce() {
cells[cells.length] = new Cell();
}}

我確實有一個 if 語句來決定它是否應該根據它擁有的食物量進行復制,所以我不會達到調用堆棧最大值。

我注意到的問題是,每次出生后產生更多細胞的細胞移動得更快,我不確定為什么。 我覺得好像創建其他單元格的單元格以某種方式連接在一起,我不確定如何解決這個問題。 我的實際代碼項目將在下面發布,任何建議將不勝感激。 還應該注意的是,我是這個網站的新手,所以任何關於如何改進我的提問方式或格式化我的問題的提示都將不勝感激。 非常感謝那些提供幫助的人。

class Creature {
    constructor(lifeSpan, pos, ms, s, r, h) {
        this.maxSpeed = ms;
        this.speed = s;
        
        this.radius = r;
        this.fat = 0;
        
        this.target = createVector(0,0);
        this.acc = createVector(0,0);
        this.vel = createVector(0,0);
        this.pos = pos;
        
        this.life = lifeSpan;
        this.lifeSpan = lifeSpan;
        this.hunger = h;
        
        this.age = 0;
    }
    
    update(i) {
        //test for death
        if (this.lifeSpan <= 0 || this.hunger <= 0) {
            this.die(i);
        }
        
        //reproduce
        if (this.age/365 > 25 && this.hunger > 10000) {
            this.reproduce();
        }
        
        //look for food
        this.findFood();
        
        //move to target
        this.move();
        
        //test for eating
        this.eat();
        
        //draw creature
        this.draw();
    }
    
    die(i) {
        console.log(this.age/365, creatures.length-1);
        creatures.splice(i, 1);
    }
    
    reproduce() {
        console.log('Made Child');
        this.hunger -= 10000;
        creatures[creatures.length] = new Creature(this.life, this.pos, this.maxSpeed, this.speed, this.radius, 1095);
    }
    
    findFood() {
        let d = pow(10, 10);
        for (let i = 0; i < food.length; i++) {
            let newD = dist(this.pos.x, this.pos.y, food[i].pos.x, food[i].pos.y);
            if (newD < d) {
                this.target = food[i].pos;
                d = newD;
            }
        }
    }
    
    move() {
        this.acc = p5.Vector.sub(this.target, this.pos);
        this.acc.setMag(this.speed);
        this.vel.add(this.acc);
        
        this.vel.y = constrain(this.vel.y, -this.maxSpeed, this.maxSpeed);
        this.vel.x = constrain(this.vel.x, -this.maxSpeed, this.maxSpeed);
        this.pos.add(this.vel);
        this.lifeSpan--;
        this.hunger--;
        this.age++;
    }
    
    eat() {
        let distance = dist(this.pos.x, this.pos.y, this.target.x, this.target.y);
        if (distance < this.radius/2 - 2.5) {
            this.hunger += 365;
            this.r++;
        }
    }
    
    draw() {
        ellipse(this.pos.x, this.pos.y, this.radius);
    }
}

class Food {
    constructor() {
        this.r = 5;
        this.pos = createVector(round(random(this.r, width-this.r)), 
        round(random(this.r, height-this.r)));
    }
    
    update() {
        //test if eaten
        for (let i = 0; i < creatures.length; i++) {
            let d = dist(creatures[i].pos.x, creatures[i].pos.y, this.pos.x, this.pos.y);
            if (d < creatures[i].radius/2 - this.r/2) {
                this.pos = createVector(random(this.r, width-this.r), 
                random(this.r, height-this.r));;
            }
        }
        
        //draw food
        ellipse(this.pos.x, this.pos.y, this.r);
    }
}

let creatures = [];
let food = [];

let time = 0;
function setup() {
    createCanvas(windowWidth, windowHeight);
    for (let i = 0; i < 30; i++) {
        let lifeSpan = round(random(18000, 32850));
        let r = round(random(10, 30));
        
        let y = round(random(r, height-r));
        let x = round(random(r, width-r));
        let pos = createVector(x, y);
        
        let ms = round(random(1, 5));
        let s = round(random(5, 20))/100;
        let h = round(random(545, 2190));
        creatures[i] = new Creature(lifeSpan, pos, ms, s, r, h);
        
        food[i] = new Food();
    }
}

function draw() {
    background(50);
    
    for (let i = 0; i < food.length; i++) {
        food[i].update();
    }
    for (let i = creatures.length-1; i >= 0; i--) {
        creatures[i].update(i);
    }
    time++;
}

如果沒有看到向量 class,我不能 100% 確定但是。 我相信您正在傳遞 pos 的矢量作為參考。 像這樣重現解構 this.pos 時嘗試做這樣的事情{...this.pos}

 creatures[creatures.length] = new Creature(this.life, {...this.pos}, this.maxSpeed,this.speed, this.radius, 1095);

更好的是也許只是嘗試像這樣從 x 和 y 創建一個新的向量

creatures[creatures.length] = new Creature(this.life, createVector(this.pos.x,this.pos.y), this.maxSpeed,this.speed, this.radius, 1095);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM