简体   繁体   English

我正在尝试将 object 添加到 object 中的数组末尾

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

I have some code that creates a bunch of Creatures(cells) and they move around the screen and eat food.我有一些代码可以创建一堆生物(细胞),它们在屏幕上移动并吃东西。 They all have an age variable so once they become a certain age they attempt to clone themselves but I don't have a complete understanding of how this should work or how to go about solving this problem and I have not been able to find something about it online.他们都有一个年龄变量,所以一旦他们达到一定年龄,他们就会尝试克隆自己,但我不完全了解这应该如何工作或如何解决这个问题 go 我一直无法找到关于它在线。

It looks something like this -->它看起来像这样 -->

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

I do have an if statement that decides if it should reproduce or not depending on the amount of food it has so I don't hit the call stack max.我确实有一个 if 语句来决定它是否应该根据它拥有的食物量进行复制,所以我不会达到调用堆栈最大值。

The problem's I've noticed are that the cell that created more cells move faster after each birth and I'm not sure as to why.我注意到的问题是,每次出生后产生更多细胞的细胞移动得更快,我不确定为什么。 I feel as though somehow the cell that created the other cells are connected in someway and I'm not sure how to fix this problem.我觉得好像创建其他单元格的单元格以某种方式连接在一起,我不确定如何解决这个问题。 My actual code project will be posted below, any suggestions would be greatly appreciated.我的实际代码项目将在下面发布,任何建议将不胜感激。 It should also be noted that I am new to this website so any tips on how I could improve the way I ask or format my questions would be greatly appreciated.还应该注意的是,我是这个网站的新手,所以任何关于如何改进我的提问方式或格式化我的问题的提示都将不胜感激。 Thanks much to those that help.非常感谢那些提供帮助的人。

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++;
}

I'm not 100% sure without seeing the vector class But.如果没有看到向量 class,我不能 100% 确定但是。 I believe you are passing the vector for pos in as a reference.我相信您正在传递 pos 的矢量作为参考。 try doing something like this when reproducing deconstruct this.pos like so {...this.pos}像这样重现解构 this.pos 时尝试做这样的事情{...this.pos}

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

even better yet maybe just try creating a new vector from the x and y like this更好的是也许只是尝试像这样从 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.

相关问题 我正在尝试遍历一个数组并找到在属性中具有所述关键字的每个对象 - I am trying to loop through an array and find each object that has said keyword in a property 我正在尝试从数组内的对象中推送值 - I am trying to push values from an object inside an array 我试图将一个对象添加到我存储在我的状态对象中的嵌套数组中,但似乎有问题 - I am trying to add an object to a nested array I have stored in my state object, but seem to have an issue with it 我正在尝试向 js object 添加一个元素 - I am trying to add an element to a js object 我正在尝试根据 cursor 和所述 object 之间的距离调整 object 的大小。 我得到的 Y 值总是 naN - I am trying to make an object resize based on the distance between the cursor and said object. The Y value I get is always naN 在数组中每个 object 末尾添加新键 - Add New key at the end of each object inside an array 我正在尝试从对象数组中检索数字数组 - I am trying to retrieve an array of numbers from an array of object 我正在尝试使用 aa for 循环将 object 键值添加到空数组进行过滤,我不断得到一个空数组 - I am trying to add an object key value to an empty array using a a for loop to filter, I keep getting an empty array 我正在尝试使用 React 中的 usestate 将元素添加到 object? - I am trying to add elements to object using usestate in React? 将 object 添加到数组反应的末尾 - Add object to end of array react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM