问题描述
我正在尝试创建一个移动的装饰圆阵列,一旦圆从屏幕边缘移开,我想将其从阵列中移除。我已经尝试从数组中进行拼接和过滤——但是,一旦我这样做,所有数组中的圆圈都会被删除,而不仅仅是预期的圆圈。检查了多个来源并尝试了不同的想法,但我根本无法弄清楚出了什么问题。您可以在 p5.js 编辑器中查看此处的演示:https://editor.p5js.org/devalladares/sketches/v6bWAiISM
如果有帮助,这里的主要代码:
function setup() {
//Create new Circle
for (let i = 0; i < circleNumber; i++) {
circles.push(new createCircle())
}
}
function draw() {
for (let i = circles.length - 1; i >= 0; i--) {
circles[i].update(i * 200)
//This is the problem: Simply cannot figure it out
//Splicing
if (circles[i].isDead()) {
circles.splice(i,1)
}
}
//Filtering
// circles = circles.filter(x => !x.isDead())
}
//Class to create Circle
class createCircle {
constructor() {
this.circle = []
this.origin = i * gap + emptySpace2
this.pos = 0
this.xPos = width / 5
this.create()
}
create() {
for (let i = 0; i < ringNumber; i++) {
this.circle.push(new circleRing())
}
}
update(dist) {
push()
this.xPos -= xSpeed
this.pos = this.xPos + dist / 1.8
translate(this.pos,this.origin / 1.15)
scale(0.6,0.6)
for (let circles of this.circle) {
circles.update()
}
pop()
}
isDead() {
if (this.pos < 40) {
return true;
} else {
return false;
}
}
}
提前致谢!!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)