简体   繁体   English

无法读取函数中未定义的属性“0”(p5.js)

[英]Cannot read property '0' of undefined in a function (p5.js)

At the first line of my code I put var array = [] but I get the error pointing towards this function.在我的代码的第一行,我放了var array = []但我得到了指向这个函数的错误。 If I log array before calling the function its exactly what it's supposed to be but if I do that anywhere inside the function it doesn't get logged before the error如果我在调用函数之前记录数组它应该是什么,但是如果我在函数内部的任何地方这样做,它在错误之前不会被记录

 let elements = 25 var array = [] let goal = [] let tempAr = [] let i = 1 function setup() { // put setup code here createCanvas(600, 600) background(25) // make the goal array ascending while (goal.length < elements) { goal.push(i) i++ } // make the scrambled array i = 0 tempAr = goal while (i < elements) { let rng = Math.floor(random(tempAr.length)) array.push(tempAr[rng]) tempAr.splice(rng, 1) i++ } } function draw() { fill('#f1f442') drawRect() sort() } function drawRect() { i = 1 while (i <= elements) { rect(i * (width / elements) - (width / elements), height - array[i - 1] * height / elements, width / elements, array[i - 1] * height / elements) i++ } } function sort() { let sorted = false while (!sorted) { sorted = true var e = 0 while (e < array.length) { if (array[e] > array[e + 1]) { let temp = array[e + 1] array[e + 1] = array[e] array[e] = temp sorted = false } drawRect() e++ } } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.js"></script>

sort() is the name of a built-in function in p5.js . sort()p5.j​​s 中内置函数的名称
You've to choose a different name for your function.您必须为您的函数选择一个不同的名称。 So rename sort to something else (eg mysort ) to make your code run.因此,将sort重命名为其他名称(例如mysort )以使您的代码运行。

 let elements = 25 var array = [] let goal = [] let tempAr = [] let i = 1 function setup() { // put setup code here createCanvas(600, 600) background(25) // make the goal array ascending while (goal.length < elements) { goal.push(i) i++ } // make the scrambled array i = 0 tempAr = goal while (i < elements) { let rng = Math.floor(random(tempAr.length)) array.push(tempAr[rng]) tempAr.splice(rng, 1) i++ } } function draw() { fill('#f1f442') drawRect() mysort() } function drawRect() { i = 1 while (i <= elements) { rect(i * (width / elements) - (width / elements), height - array[i - 1] * height / elements, width / elements, array[i - 1] * height / elements) i++ } } function mysort() { let sorted = false while (!sorted) { sorted = true var e = 0 while (e < array.length) { if (array[e] > array[e + 1]) { let temp = array[e + 1] array[e + 1] = array[e] array[e] = temp sorted = false } drawRect() e++ } } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.js"></script>

暂无
暂无

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

相关问题 p5.j​​s 27646:未捕获的类型错误:无法读取未定义的属性“0” - p5.js 27646: Uncaught TypeError: Cannot read property '0' of undefined p5.j​​s上的“未捕获的TypeError:无法读取未定义的属性&#39;bind&#39;” - “Uncaught TypeError: Cannot read property 'bind' of undefined” on p5.js 括号p5.js“未捕获的TypeError:无法读取未定义的属性&#39;offscreen&#39;” - Brackets p5.js “Uncaught TypeError: Cannot read property 'offscreen' of undefined” TypeError:在p5.js中使用image()时,无法读取未定义的属性“width” - TypeError: Cannot read property 'width' of undefined when using image() in p5.js p5.js web 编辑器。 '未捕获的类型错误:无法读取未定义的属性'拆分'(:第 57 行)' - p5.js web editor. ' Uncaught TypeError: Cannot read property 'split' of undefined (: line 57)' 未捕获的类型错误:无法读取未定义的属性“图像”(p5.js) - Uncaught TypeError: Cannot read property 'image' of undefinded (p5.js) 谁能告诉我为什么我收到错误'无法读取未定义的属性'值''? P5.JS - Can anyone tell me why i am getting the error 'Cannot read property 'value' of undefined'? P5.JS 使用p5.js在JavaScript中未定义JSON属性 - JSON Property undefined in JavaScript with p5.js 无法读取未定义的属性-p5js - Cannot read property of undefined - p5js p5.js 出现错误:未捕获的类型错误:无法读取 null 的属性“transpose3x3” - p5.js getting error: Uncaught TypeError: Cannot read property 'transpose3x3' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM