简体   繁体   English

谁能帮我找出为什么我的 p5 草图每次运行时都会崩溃?

[英]Can anybody help me find out why my p5 sketch is crashing every time I run it?

Sadly, I can't put all of the code in this window, but here's the link to the project: https://editor.p5js.org/thing1/sketches/KIsvdFvPt遗憾的是,我不能将所有代码都放在这个 window 中,但这里是项目的链接: https://editor.p5js.org/thing1/sketches/KIsvdFvPt

You can duplicate it to edit it and look at all of the files if you so wish.如果愿意,您可以复制它以对其进行编辑并查看所有文件。 I'm sorry this is such a vague question, but I've worked so hard on this program and now it feels like it's all been for nothing.很抱歉,这是一个如此模糊的问题,但我为这个程序付出了如此多的努力,现在感觉一切都白费了。 Any help is appreciated:) I really appreciate any help you can provide.感谢您提供任何帮助:) 我非常感谢您提供的任何帮助。 Many thanks!非常感谢!

Firstly, as mentioned in the comments you're removing from an array whilst iterating through it.首先,如评论中所述,您在遍历数组时要从数组中删除。 This is often needed in game development and the common practice is to iterate through the array backwards:这在游戏开发中经常需要,通常的做法是向后迭代数组:

for (let i = comets.length - 1; i >= 0; i--) {
  // do your removing
}

However, this is only part of the problem.然而,这只是问题的一部分。 The reason your sketch is freezing, is because you've got the following code in your Comet class:您的草图冻结的原因是因为您的Comet class 中有以下代码:

polygon(this.x,this.y,this.health*2,this.health/5)

So you're setting the radius and npoints of the polygon to values dependent upon the health of the asteroid, you can imagine why it doesn't like it when a negative value is set!因此,您将多边形的radiusnpoints设置为取决于小行星健康状况的值,您可以想象为什么设置负值时它不喜欢它!

Wrap this code in a simple if:将此代码包装在一个简单的 if 中:

if (this.health > 0) {
  polygon(this.x, this.y, this.health*2, this.health/5)
}

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

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