简体   繁体   English

P5.js object 不留痕迹

[英]P5.js object not leaving a trail

I have a project in which I'm trying to make a helix effect, where two ellipses rotate around each other, and they leave a trail which keeps going downwards and then disappearing.我有一个项目,我正在尝试制作螺旋效果,两个椭圆围绕彼此旋转,它们留下一条不断向下然后消失的轨迹。 now the problem is, I cant get the trail to show up.现在的问题是,我无法显示踪迹。 I followed a tutorial by the coding train on making a trail, and while it works for him, it doesn't for me.我按照编码火车的教程进行了跟踪,虽然它对他有用,但对我不起作用。 one thing that I did differently from him is that he was using a classes for his ball, while I'm not.我做的与他不同的一件事是他使用类来处理他的球,而我没有。

My trail code's like this.我的跟踪代码是这样的。 It takes in a array I made called history, which contains a 2d vector containing the x and y positions, and it should be making a copy of the ellipse every frame, but instead it just makes a new ellipse and erases the last one.它接收我创建的一个名为 history 的数组,其中包含一个包含 x 和 y 位置的 2d 向量,它应该每帧都制作一个椭圆的副本,但它只是制作一个新的椭圆并擦除最后一个椭圆。

function makeTrail(){
    history.push(pos1);
    for (var i = 0; i < history.length; i++){
        let p = history[i];
        ellipse(p.x, p.y, 8, 8);
    }
 }

and here's my draw function if it helps.如果有帮助,这是我的抽奖 function。 Most of it's just code for where I want the balls to be drawn.其中大部分只是我想要绘制球的位置的代码。 The MoveBall functions just tell the program what to do with the balls. MoveBall函数只是告诉程序如何处理球。

function draw(){
    pos1.y += -1;
    pos2.y += -1;
    let rs = 30/*random(10, 30)*/;
    fill('#f42069');
    ellipse(pos1.x, pos1.y, rs, rs);
    moveBall();
    fill('#b4da22');
    ellipse(pos2.x, pos2.y, rs, rs);
    moveBall2();
    makeTrail();
}

The problem is that you're pushing pos1 into history , when you should be pushing pos1.copy() .问题是您将pos1推入history ,而您应该推pos1.copy() When you push the vector itself, the value in the list changes with the value of pos1 .当您推送向量本身时,列表中的值会随着pos1的值而变化。

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

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