简体   繁体   English

p5.j​​s 27646:未捕获的类型错误:无法读取未定义的属性“0”

[英]p5.js 27646: Uncaught TypeError: Cannot read property '0' of undefined

I was coding in JavaScript, p5.js and came across this error line:我在 JavaScript、p5.js 中编码,遇到了这个错误行:

27646: Uncaught TypeError: Cannot read property '0' of undefined 27646:未捕获的类型错误:无法读取未定义的属性“0”

<\/blockquote>

and I don't know what this means since I am not very experienced with JavaScript.而且我不知道这意味着什么,因为我对 JavaScript 不是很有经验。 It is also suspicious to me because my project does not have line 27646 anywhere.这对我来说也很可疑,因为我的项目在任何地方都没有第 27646 行。

here is my code:这是我的代码:

 var particles = []; const word0 = 'p5.js', word1 = 'is nice'; var active, inactive; var cooldown; var font; function preload() { font = loadFont('data\/LucidaSansRegular.ttf'); } function setup() { createCanvas(600, 300); active = font.textToPoints(word0, 50, 200, 200); inactive = font.textToPoints(word1, 50, 200, 200); cooldown = 500; for (var i = 0; i < active.length; i++) { particles.push(new Particle(active[i].x, active[i].y)); } } function draw() { background(60); noStroke(); fill(255); for (var i = 0; i < particles.length; i++) { if (particle[i].radius < 0) { particles.splice(i, 1); continue; } if (cooldown < 0) { particles[i].forceToDes(); } else { particles[i].fleeFromDes(); } particles[i].update(); particles[i].show(); } if (cooldown < 500 && cooldown > -500) { cooldown--; } else { cooldown++; } if (cooldown == -499) { changeText(); } } function changeText() { switchActive(); if (active.length == inactive.length) { for (var i0 = 0; i0 < active.length; i0++) { particles[i0].des = createVector(active[i0].x, active[i0].y) } } else if (active.length > inactive.length) { for (var i1 = 0; i1 < particles.length; i1++) { particles[i1].des = createVector(active[i1].x, active[i1].y); } for (var i2 = 0; i2 < active.length - inactive.length; i2++) { var p = new Particle(active[i2].x, active[i2].y), r = particles.random(); p.pos = r.pos; particles.push(p); } } else { for (var i3 = 0; i3 < inactive.length - active.length; i3++) { var r = floor(random(particles.length)); particles[r].shrink(); } for (var i4 = 0; i4 < particles.length; i4++) { particles[i4].des = createVector(active[i4].x, active[i4].y); } } } function switchActive() { var temp = active; active = inactive; inactive = temp; }<\/code><\/pre>

I hope you can help me :) Thank you already!我希望你能帮助我:) 已经谢谢你了!

"

Please post your code as an MCVE that we can copy and paste to run. 请以MCVE的形式发布您的代码,我们可以将其复制并粘贴以运行。 When I try to run your code, I get an error because I don't have the font you're trying to load. 当我尝试运行您的代码时,出现错误,因为我没有要加载的字体。 Either include the font file or modify your code so we don't need the font file. 要么包含字体文件,要么修改您的代码,因此我们不需要字体文件。

But just looking at your code, this sticks out to me: 但是,仅查看您的代码,这对我很明显:

for (var i = 0; i < particles.length; i++) {
  if (particle[i].radius < 0) {

Here you're looping over the particles array, but then using a variable called particle . 在这里,您遍历了particles数组,但随后使用了一个名为particle的变量。 This is probably a simple typo. 这可能是一个简单的错字。

I'm also suspicious of this line: 我对此行也表示怀疑:

r = particles.random();

Here you're setting a variable that you haven't defined yet. 在这里,您要设置一个尚未定义的变量。

When your code contains multiple errors like this, it usually means that you aren't testing in small chunks. 当您的代码包含这样的多个错误时,通常意味着您不是在小块中进行测试。 You should really get into the habit of working in isolation, only testing one small part until you get that working perfectly. 您应该真正养成孤立工作的习惯,只测试一小部分,直到您可以正常工作。 That also makes it easier to post an MCVE if you do get stuck. 如果确实遇到问题,这也使发布MCVE更容易。

Your problem is that your word1 string has a space in it. 您的问题是您的word1字符串中有一个空格。

textToPoints() doesn't handle non-printable characters. textToPoints()不处理不可打印的字符。

Try a string with no space or split your string up into to strings and combine the points arrays afterwards. 尝试使用没有空格的字符串,或者将您的字符串拆分为多个字符串,然后再合并点数组。

On Line 1 You define:在第 1 行您定义:

var particles = [];

暂无
暂无

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

相关问题 p5.j​​s上的“未捕获的TypeError:无法读取未定义的属性&#39;bind&#39;” - “Uncaught TypeError: Cannot read property 'bind' of undefined” on p5.js 未捕获的类型错误:无法读取未定义的属性“图像”(p5.js) - Uncaught TypeError: Cannot read property 'image' of undefinded (p5.js) 括号p5.js“未捕获的TypeError:无法读取未定义的属性&#39;offscreen&#39;” - Brackets p5.js “Uncaught TypeError: Cannot read property 'offscreen' of undefined” p5.js web 编辑器。 '未捕获的类型错误:无法读取未定义的属性'拆分'(:第 57 行)' - p5.js web editor. ' Uncaught TypeError: Cannot read property 'split' of undefined (: line 57)' p5.js 出现错误:未捕获的类型错误:无法读取 null 的属性“transpose3x3” - p5.js getting error: Uncaught TypeError: Cannot read property 'transpose3x3' of null TypeError:在p5.js中使用image()时,无法读取未定义的属性“width” - TypeError: Cannot read property 'width' of undefined when using image() in p5.js 无法读取函数中未定义的属性“0”(p5.js) - Cannot read property '0' of undefined in a function (p5.js) p5.js 父() | 未捕获(承诺中)类型错误:无法读取 null 的属性(正在读取“appendChild”) - p5.js parent() | Uncaught (in promise) TypeError: Cannot read properties of null (reading 'appendChild') 收到错误“ p5practice.js:97 Uncaught TypeError:无法读取未定义的属性&#39;y&#39;” - getting an error “p5practice.js:97 Uncaught TypeError: Cannot read property 'y' of undefined” Chaplin.js-未捕获的TypeError:无法读取未定义的属性“未定义” - Chaplin.js - Uncaught TypeError: Cannot read property 'undefined' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM